Custom class problem (strings)
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Custom class problem (strings)

  1. #1
    Join Date
    Jan 2009
    Posts
    1

    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
    Code:
    #ifndef WORD_H
    #define WORD_H
    
    class Word
    {
    	public:
    		Word( string );
    		string get( void );
    	private:
    		string i;
    };
    
    #endif
    word.cpp
    Code:
    #include "word.h"
    #include <string>
    
    Word::Word(string ii)
    {
    	i = ii
    }
    
    string Word::get( void )
    {
    	return i;
    }
    main.cpp
    Code:
    #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!

  2. #2
    Join Date
    Apr 1999
    Posts
    27,418

    Re: Custom class problem (strings)

    Code:
    #ifndef WORD_H
    #define WORD_H
    
    class Word
    {
    	public:
    		Word( string );
    		string get( void );
    	private:
    		string i;
    };
    
    #endif
    The compiler has no idea what "string" is. You didn't include the proper header:
    Code:
    #ifndef WORD_H
    #define WORD_H
    
    #include <string>
    class Word
    {
    	public:
    		Word( std::string );
    		std::string get( void );
    	private:
    		std::string i;
    };
    #endif
    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center