How to initial "static const char * []" in class member?[C++] - c++

When I am doing my homework, the question ask me to finish the given class:
//Card.hpp
class Card {
private:
static const char* faceNames[totalFaces];
};
I write mycode in Card.cpp:
const char* faceNames[Card::totalFaces] = { "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
I use VS2017 to build it, but it shows that
error LNK2001: unresolved external symbol "private: static char const * * Card::faceNames" (?faceNames#Card##0PAPBDA)
How should I do?

You can define it outside the class without specifying the size. You don't have to specify the size again. Note the :: requirement for defining members outside class.
const char* Card::faceNames[] = { "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
Demo

You accidentally defined an unrelated array faceNames when you meant to define the static member. You need to name the class static member using :: notation:
const char* Card::faceNames[Card::totalFaces] = { "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };

Related

How to access the arrays in private class of C++?

class bus {
private:
string arr[10] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
public:
void reservation();
};
Here, I have a private array that I want to access and make changes through the reservation() in public class.
void bus::reservaton() {
cout << "What should I write in here to change the 3rd and 7th index of the
above array to \"not empty\"" << endl;
}
Suppose, I want to make the 3rd and 7th index to "not empty", what should I write there?
Sample:-
string arr[10] = { "0", "1", "2", "not empty", "4", "5", "6", "not empty", "8", "9" };
And do I need to make any changes in the main function? If yes, then can you please help me by writing it down.
Thank you.
How to use arrays
void bus::reservation() {
arr[3] = "not empty";
arr[7] = "not empty";
}
For this, a simple assignment operator (=) would work.
For example, in the following example, the value at index 1 is changed:
std::string slist[ 3 ] { "a", "b", "c" };
slist[ 1 ] = "Changed!";
Live example: http://ideone.com/rO9hwt
BTW, you should use std::vector of std::string instead of an array. And, take a look at its member function at() for accessing the value of an index with out of bounds checking.
Live example: http://ideone.com/bcHZpY

Make assoc-id for collection conditionally in clojure

I made assoc-in like this
(defn process-pubaccess-nb [conn books]
(map #(assoc-in % [:book/publication :publication/access] (get-rules-ids % conn) ) books)
)
I want to add condition don't made assoc-in if get-rules-ids returns nil. I tried to add when-let, but I had errors.
For example if I get
(def lib [{:book/name "one" :book/pid "1" :book/publication {:publication/pid "11"} }
{:book/name "two" :book/pid "2":book/publication {:publication/pid "22"} }
{:book/name "three" :book/pid "3" }])
I want to have
({:book/name "one", :book/pid "1", :book/publication {:publication/pid "11", :publication/access "test"}}
{:book/name "two", :book/pid "2", :book/publication {:publication/pid "22", :publication/access "test"}}
{:book/name "three", :book/pid "3"})
Now I have without condition
({:book/name "one", :book/pid "1", :book/publication {:publication/pid "11", :publication/access "test"}}
{:book/name "two", :book/pid "2", :book/publication {:publication/pid "22", :publication/access "test"}}
{:book/name "three", :book/pid "3", :book/publication {:publication/access nil}})
Maxx
Amar's solution works but it isn't clear the cost of calling get-rules-ids versus pre-filtering the collection.
Here is the filtered option for posterity:
(defn process-pubaccess-nb
[conn books]
(map #(assoc-in % [:book/publication :publication/access] (get-rules-ids % conn))
(filter :book/publication books)))
Also, if the collection may be considerable in size, transducers could be more performant.
One way to do this is with if-let.
(defn process-pubaccess-nb
[conn books]
(map #(if-let [access (get-rules-ids % conn)]
(assoc-in % [:book/publication :publication/access] access)
%)
books))

rapidjson cocos2d-x parsing

rapidjson::Document d;
d.Parse<0>(chatevent.chat.c_str());
if(d.HasMember("kelimeler"))
{
rapidjson::Value::MemberIterator M;
const char *key,*value;
for (M=d.MemberBegin(); M!=d.MemberEnd(); M++)
{
key = M->name.GetString();
value = M->value.GetString();
if (key!=NULL && value!=NULL)
{
log("key: %s, value: %s", key,value);
}
}
}
This is the code i use to handle json data in cocos2d-x. And here is the json:
{
"kelimeler": [{
"harfsayisi": 10,
"kelime": "bibnştvdaf",
"harfler": ["t", "s", "ç", "p", "b", "c", "h", "n", "c", "c", "n", "b", "t", "v", "ş", "v", "a", "c", "v", "p", "d", "ğ", "s", "k", "i", "ç", "f", "v", "b", "p", "a", "ü", "d", "ü", "e"]
}]
}
So how to handle it using the code? I simply can not get the "kelimeler" branch. Thanks in advance.
Need to remember to watch out for namespace collisions. Ideally the compiler warns about ambiguity. In your case you need to specify you want the classes from rapidjson and not cocos2d.
Updating Josh's example.
rapidjson::Document d;
d.Parse<0>(chatevent.chat.c_str());
if(d.HasMember("kelimeler"))
{
const rapidjson::Value& k = d["kelimeler"]; // you are missing this
assert(k.IsArray());
if(k.HasMember("harfler"))
{
const rapidjson::Value& h = k["harfler"];
for (rapidjson::SizeType i = 0; i < h.Size(); i++)
{
log("value: %s", h[i].GetString());
}
}
}
No where in you're code does it actually get the "kelimeler" array. See the rapidjson tutorial http://rapidjson.org/md_doc_tutorial.html#ValueDocument
if(d.HasMember("kelimeler"))
{
const Value& k = d["kelimeler"]; // you are missing this
assert(k.IsArray());
for (SizeType i = 0; i < k.Size(); i++)
{
...
}
}

Initialize two dimensional array with different columns sizes in c++ [duplicate]

This question already has answers here:
Do jagged arrays exist in C/C++?
(12 answers)
Closed 8 years ago.
Is there a way to initialize two dimensional array with different size of columns in c++?
I try to make console card game.
I have done something like this while ago.
static string const group[7][];
string const Cards::group = {
{ "AA", "KK", "AKs" },
{ "QQ", "JJ", "AK", "AJs", "KQs", "AQs" },
{ "TT", "AQ", "ATs", "KJs", "QJs", "JTs" },
{ "99", "88", "AJ", "AT", "KQ", "KTs", "QTs", "J9s" , "T9s" , "98s" },
{ "77", "66", "A9s", "A5s", "A4s", "A3s", "A2s", "K9s", "KJ", "KT", "QJ", "QT", "Q9s", "JT", "QJ", "T8s", "97s", "87s", "76s", "65s"},
{ "55", "44", "33", "22", "K9", "J9", "86s"},
{ "T9", "98", "85s"}
};
It doesn't work.
It would be great if an array would be additionally static and constant but it isn't necessary. The most important thing is to make code works.
I would be grateful for any kind of help.
Arrays in C++ are not truly dynamic; they cannot be extended nor reduced in size. Moreover, the dimensions of static arrays have to be known at their declaration. Dynamically-allocated arrays can be used by declaring group as a string** and later initializing it with the appropriately-sized memory. You can also use a 2-dimensional vector which is fundamentally equivalent but preferred because it is cleaner because we can leverage its memory management:
static std::vector<std::vector<std::string>> const group;
std::vector<std::vector<std::string>> Cards::group = {
{ "AA", "KK", "AKs" },
{ "QQ", "JJ", "AK", "AJs", "KQs", "AQs" },
{ "TT", "AQ", "ATs", "KJs", "QJs", "JTs" },
{ "99", "88", "AJ", "AT", "KQ", "KTs", "QTs", "J9s" , "T9s" , "98s" },
{ "77", "66", "A9s", "A5s", "A4s", "A3s", "A2s", "K9s", "KJ", "KT", "QJ", "QT", "Q9s", "JT", "QJ", "T8s", "97s", "87s", "76s", "65s"},
{ "55", "44", "33", "22", "K9", "J9", "86s"},
{ "T9", "98", "85s"}
};
You can't use:
string group[7][] =
However, you can use:
std::vector<std::string> group[] =
Here's a working program.
#include <iostream>
#include <string>
#include <vector>
std::vector<std::string> group[] =
{
{ "AA", "KK", "AKs" },
{ "QQ", "JJ", "AK", "AJs", "KQs", "AQs" },
{ "TT", "AQ", "ATs", "KJs", "QJs", "JTs" },
{ "99", "88", "AJ", "AT", "KQ", "KTs", "QTs", "J9s" , "T9s" , "98s" },
{ "77", "66", "A9s", "A5s", "A4s", "A3s", "A2s", "K9s", "KJ", "KT", "QJ", "QT", "Q9s", "JT", "QJ", "T8s", "97s", "87s", "76s", "65s"},
{ "55", "44", "33", "22", "K9", "J9", "86s"},
{ "T9", "98", "85s"}
};
int main()
{
for ( auto it1 : group )
{
for ( auto it2 : it1 )
{
std::cout << it2 << " ";
}
std::cout << std::endl;
}
}
You cannot do
string group[7][];
Instead, you can only do
string group[][7];
for multidimensional C-style arrays.
If you truly need multidimensional array with different sizes in the last dimension, you can use std::vector> with STL.

Reorder a list based on example list

I would like to know how to reorder a list of lists based on an example list.
Here is an example to clarify my question:
["Foo", "Bar", "Something"]
That is example list, now I want to reorder the list shown below, looking only at the heads of each list inside it:
[["Something", "one", "two", "three"], ["Foo", "four", "five", "six"],
["Bar", "seven", "eight", "nine"]]
to this:
[["Foo", "four", "five", "six"], ["Bar", "seven", "eight", "nine"],
["Something", "one", "two", "three"]]
Thanks
Edit:
I've tried mapping a swap function that swaps two elements, but that won't work because I am dealing here with two lists.
Probably a very brute approach:
Prelude> let a = [["Something", "one", "two", "three"], ["Foo", "four", "five", "six"], ["Bar", "seven", "eight", "nine"]]
Prelude> let b = ["Foo", "Bar", "Something"]
Prelude> concatMap (\y -> filter (\(x:xs) -> x == y) a) b
[["Foo","four","five","six"],["Bar","seven","eight","nine"],["Something","one","two","three"]]
Another solution based on list comprehension can be :
Prelude> let a = [["Something", "one", "two", "three"], ["Foo", "four", "five", "six"], ["Bar", "seven", "eight", "nine"]]
Prelude> let b = ["Foo", "Bar", "Something"]
Prelude> let c = [ y | x<-b, y<-a, x `elem` y ]
[["Foo","four","five","six"],["Bar","seven","eight","nine"],["Something","one","two","three"]]
you can read this chapter for more explanation on how to use list comprehension: Lyah
You want to sort your list according to a custom comparison. So, let's do it! The basic comparison function should look at the index a value appears in the examples list.
import Data.List
cmpIndex :: Eq a => [a] -> [a] -> [a] -> Ordering
cmpIndex example s1 s2 = compare (indexOf s1) (indexOf s2)
where indexOf s = findIndex (head s ==) example
This is a little dangerous -- calling head is always a cause to pause. But let's assume you know something I don't and move on. We'll give names to our inputs to make the test more readable, then fire up ghci:
example = ["Foo", "Bar", "Something"]
list = [["Something", "one", "two", "three"], ["Foo", "four", "five", "six"], ["Bar", "seven", "eight", "nine"]]
*Main> sortBy (cmpIndex example) list
[["Foo","four","five","six"],["Bar","seven","eight","nine"],["Something","one","two","three"]]