Hello, I'm getting some errors in my code and I'm having some trouble debugging it.
Here's the errors:
~/cs3350mp$ g++ set.cpp
set.cpp: In member function 'void CharSet::insert(const char*)':
set.cpp:48: error: expected primary-expression before '[' token
set.cpp:49: error: expected primary-expression before '[' token
set.cpp: In member function 'std::string CharSet::toString()':
set.cpp:57: error: expected primary-expression before '[' token
set.cpp:58: warning: control reaches end of non-void function
And the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//LINE 45 // insert a null-terminated (c-string) of characters
// into the set
//47 void insert(const char chars[]) {
for (int i = 0; string[i]; i++)
//49 isInSet[string[i]] = true;
}
// return a string containing all of the characters in
// this set in ASCII-value order
// note: this will act funny with non-printable characters
//55 string toString() {
for (int i = 0; i < SIZE; i++)
//57 return string[i] = i;
}
If you have written usingnamespace std; the compiler will think you are referring to std::string, which is a type, so you can't have a variable with this name.
I often do a similar mistake, when i try to add a free() method in a class, and the compiler thinks i try to call the free() function in the class declaration :-P
Bartoli is right. If you did type using namespace std, the compiler may think that your're trying to instantiate std::string. If string is declared globally, prefix it with the scope resolution operator (tells the compiler that string is declared globally). Another solution would be to rename string.
It doesn't seem to help, syntactically I can get it to remove those certain errors but I'm not sure what to replace it with to get it working properly. This is the whole class and I'm only getting errors on the same 2 functions because I'm not sure how to write them.
constint SIZE = 256;
class CharSet {
public:
// constructors initializes the set to empty
CharSet() {
for (int i = 0; i < SIZE; i++)
isInSet[i] = false;
}
// put a specific character into the set
// if the character is already in the set, no change
void insert(char c) {
if(!isMember(c))
isInSet[indexFromChar(c)] = true;
}
// check if a specific character in in the set
bool isMember(char c) {
return isInSet[indexFromChar(c)];
}
// remove a character from the set
// if the character is not in the set, no change
void remove(char c) {
if(isMember(c))
isInSet[indexFromChar(c)] = false;
}
//THIS IS THE FUNCTION I'M HAVING TROUBLE ON
// insert a null-terminated (c-string) of characters
// into the set
void insert(constchar chars[]) {
for (int i = 0; chars[i]; i++)
isInSet[chars[i]] = true;
}
// return a string containing all of the characters in
// this set in ASCII-value order
// note: this will act funny with non-printable characters
string toString() {
for (int i = 0; i < SIZE; i++)
isInSet[i] = i;
return isInSet;
}
private:
// maps characters to the range 0-255 in a 1-1 manner
// remember, signed chars have value -128 to 127
int indexFromChar(char c) {
return c+128;
}
// maps the range 0-255 into characters in a 1-1 manner
// remember, signed chars have value -128 to 127
char charFromIndex(int i) {
unsignedchar uc = i;
return (char)uc;
}
// an array of 256 bools, each reresents wheter a specific
// character is in the set or not
bool isInSet[SIZE];
};