Class definition Identifier problems

This is a discussion on Class definition Identifier problems within the C++ Programming forums, part of the General Programming Boards category; Ok, well this is a bit wierd, but for some reason when defining a function inside a class with the ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    69

    Class definition Identifier problems

    Ok, well this is a bit wierd, but for some reason when defining a function inside a class with the identifier "string" doesn't work, I get this error
    - error C2061: syntax error : identifier 'string'

    here's the code

    comhandle.cpp
    Code:
    #include <string>
    #include "comhandle.h"
    
    void COMHANDLE::run(string mystring)
    {
    	if(mystring!="test"){
    		bprint("you typed\""+ mystring + "\"!", true, true);
    	} else {
    		bprint("unknown command?", true, true);
    	}
    }
    main.cpp
    Code:
    #include <stdlib.h>
    #include <iostream>
    #include <fstream>
    #include <string>
    #include "comhandle.h"
    
    COMHANDLE COMHANDLEC;
    //	Namespaces
    using namespace std;
    
    //	Global Functions
    void bprint (string text, bool cneeded, bool newline);
    void binit(void);
    void brun(void);
    void bprinte(void);
    
    //	Globals Vars
    string name;
    string vers;
    string sstring;
    string cline;
    string choice;
    
    int main()
    {
    //	main() Vars
    
    binit();
    brun();
    	return 0;
    }
    void bprint (string text, bool cneeded, bool newline){
    	if(cneeded){
    		if(newline){
    			cout << cline << text <<"\n";
    		} else {
    			cout << cline << text;
    		}
    	} else {
    		if(newline){
    			cout << text << "\n";
    		} else {
    			cout << text;
    		}
    	}
    }
    void binit(void)
    {	
    
    	cline = "<<: ";
    	name = "Blade Tools v";
    	vers = "0.001";
    	sstring = name + vers;
    	bprint(sstring, false, true);
    }
    void brun(void){
    	bprint("", true, false);
    	cin >> choice;
    	COMHANDLEC.run(choice);
    }
    void bprinte(void)
    {
    	bprint ("Unknown command \""+choice+"\" - Type \"help\" for a list of valid commands.",true, true);
    }
    comhandle.h
    Code:
    #ifndef COMHANDLE_H
    
    #include <string>
    
    #define COMHANDLE_H
    
    class COMHANDLE
    {
    
    public:
    	void run(string mystring);
    
    };
    
    #endif
    main.h
    Code:
    #ifndef MAIN_H
    #define MAIN_H
    class MAIN
    {
    public:
    //to be used later
    }
    #endif

    Iv tried alot of things, but have been unsuccessful in all attempts. Any help would be appreciated. Thanks!

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    32
    Try this,
    Code:
    void run(std::string mystring);

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    69
    Quote Originally Posted by Anubis
    Try this,
    Code:
    void run(std::string mystring);
    yay that worked. Thanks! One question though. Why wont it work if I just put string?

    EDIT: wait is it because I didn't define the name space "using namespace std;"

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,620
    Yep. The standard string class lives in the namespace std. Unless you have a "using namespace std;" in effect, the unadorned name "string" can not resolve to "std::string" (or, to put it another way, std::string is not a candidate when the compiler sees the type name string.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  2. class Template problems
    By Mr_roboto in forum C++ Programming
    Replies: 8
    Last Post: 02-16-2006, 10:21 PM
  3. Replies: 8
    Last Post: 10-02-2005, 01:27 AM
  4. class errors
    By romeoz in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2003, 08:57 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21