Class problem

Jul 29, 2012 at 10:38pm
Can someone point me in the right direction please? I think I have made a simple mistake but I can't figure it out to save my life.

Thanks,
Nick


dictionary.h
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
#ifndef DICTIONARY_H
#define DICTIONARY_H

#include <string>
class Dictionary
{
public:

	Dictionary();

	string getWord();
	void setWord();

	int getWordLength();
	

private:

	string getRandomWord(string x[]);
	string wordArray[] = {"apple", "tree", "house", "desk", "monster", "fish", "lion", "dragon", "computer"};

	string currentWord;
	int currentLength;

};



#endif 



dictionary.cpp
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
#include "dictionary.h"

#include <string>
#include <time.h>
#include <iostream>

using namespace std;

Dictionary::Dictionary()
{
	setWord();
	cout << "CONSTRUCTOR" << endl;
	cout << "Current word: " << currentWord << endl;
	cout << "Current length: " << getWordLength() << endl;
}


string Dictionary::getRandomWord(string x[])
{
	srand(time(NULL));
	int y = rand() % 9; //This depends on the num of words

	return x[y];
}

void Dictionary::setWord()
{
	currentWord = getRandomWord(wordArray);
}

string Dictionary::getWord()
{
	return currentWord;
}

int Dictionary::getWordLength()
{
	return currentWord.size();

}



main.cpp
1
2
3
4
5
6
7
8
#include "dictionary.h"

int main()
{
	Dictionary dict;

	return 0;
}
Jul 29, 2012 at 10:48pm
In dictionary.h, you're trying to use string without specifying the namespace.


string wordArray[] = {"apple", "tree", "house", "desk", "monster", "fish", "lion", "dragon", "computer"}; cannot be done there in C++ 98 or 03. Either compile as C++11, or put assigning values to variables where that sort of thign belongs - in the constructor.

If you're going to use srand and rand, you need to include the right header.


Last edited on Jul 29, 2012 at 10:50pm
Jul 29, 2012 at 10:48pm
It would help if you told us what errors the compiler is giving you :/
Jul 30, 2012 at 3:58am
Thank you Moschops, declaring the namespace fixed most of the problems. The only thing left is the array like you mentioned above. I have always created arrays this way and have never had a problem before so i'm a little confused. How do specify which version of C++ I want it to compile? Is there any other reason that I would be getting errors for the array that don't have to do with the format?
Topic archived. No new replies allowed.