Custom class problem (strings)

hey all!

this might look silly but i'm having problems using strings in a custom class.

here is the code:

word.h
1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef WORD_H
#define WORD_H

class Word
{
	public:
		Word( string );
		string get( void );
	private:
		string i;
};

#endif 


word.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include "word.h"
#include <string>

Word::Word(string ii)
{
	i = ii
}

string Word::get( void )
{
	return i;
}


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "word.h"
#include <string>
#include <iostream>

using namespace std;

int main()
{
	Word c( "3" );

	cout << c.get();
	cin.get();

}

and here are the lovely errors:

Error 1 error C2460: 'Word::string' : uses 'Word', which is being defined word.h 7
Error 2 error C2146: syntax error : missing ';' before identifier 'get' word.h 8
Error 3 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int word.h 8
Error 4 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int word.h 8
Warning 5 warning C4183: 'get': missing return type; assumed to be a member function returning 'int' word.h 8
Error 6 error C2146: syntax error : missing ';' before identifier 'i' word.h 10
Error 7 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int word.h 10
Error 8 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int word.h 10
Error 9 error C2440: 'initializing' : cannot convert from 'const char [2]' to 'Word' main.cpp 9
Error 10 error C2460: 'Word::string' : uses 'Word', which is being defined word.h 7
Error 11 error C2146: syntax error : missing ';' before identifier 'get' word.h 8
Error 12 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int word.h 8
Error 13 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int word.h 8
Warning 14 warning C4183: 'get': missing return type; assumed to be a member function returning 'int' word.h 8
Error 15 error C2146: syntax error : missing ';' before identifier 'i' word.h 10
Error 16 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int word.h 10
Error 17 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int word.h 10
Error 18 error C2597: illegal reference to non-static member 'Word::string' word.cpp 4
Error 19 error C2146: syntax error : missing ')' before identifier 'ii' word.cpp 4
Error 20 error C2761: '{ctor}' : member function redeclaration not allowed word.cpp 4
Error 21 error C2059: syntax error : ')' word.cpp 4
Error 22 error C2143: syntax error : missing ';' before '{' word.cpp 5
Error 23 error C2447: '{' : missing function header (old-style formal list?) word.cpp 5
Error 24 error C2143: syntax error : missing ';' before 'Word::get' word.cpp 9
Error 25 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int word.cpp 9
Error 26 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int word.cpp 10
Error 27 error C2065: 'i' : undeclared identifier word.cpp 11

can anyone make sense of these errors and why they are occurring? everything looks fine to me syntactically. am i missing includes in the word.h file?

thanks!
Last edited on
The word.h file requires #include <string> after your #define directive. You can remove #include <string> from word.cpp because it then comes along with word.h.

~psault
Last edited on
thanks for your reply!

however adding #include <string> after #define in the word.h only adds more errors :( i tried adding the include when i was debugging and it still didn't solve the problem. by the way i'm using Visual Studio 2008 to complie.

could it be anything else?
Oh yes, in every file that you did not use "using namespace std" you need to change string to std::string because string is part of the std namespace and cannot be seen by the compiler without that scope operator.

~psault
never mind!

using namespace std;

that was missing. i feel like such a noob! :D

thanks for your help!
Don't fret. Many many people advise against using "using namespace std" and suggest that you use the scope operator instead. That means typing std:: in front of every function that is within the std namespace you are trying to access.
Kind of a pain, but it is "the point of namespaces" to allow for duplicate identifiers wrapped in different namespaces without getting a compiler error.

~psault
I have a new lovely error, is unknown to you....
I think, maybe my problem message is so strange as it says:

[code=cplusplus]Error: Error: Unresolved external 'WORD::get( void )__stdcall' referenced from MAIN.OBJ[/code]

what's a problem?
Topic archived. No new replies allowed.