Help me debug this error?

Aug 26, 2011 at 11:58pm
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;
			}
Aug 27, 2011 at 12:49pm
If you have written using namespace 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
Aug 27, 2011 at 2:07pm
closed account (zb0S216C)
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.

Wazzak
Aug 28, 2011 at 6:01pm
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const int 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(const char 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) {
		unsigned char 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];
};


Last edited on Aug 29, 2011 at 5:58pm
Aug 29, 2011 at 8:56pm
Any ideas? I might have to turn it in as is.
Topic archived. No new replies allowed.