tags:

views:

58

answers:

4

hello everyone, total newbie is here :) i have this header file, zeeheader.h, and i wrote some classes in it, i'm having problems giving a string as a parameter to one of the functions:

class DeliTest
{

public:

    void DeliCheck(Stack*,string);
    void ComCheck (unsigned,string);
    bool EofCheck (unsigned,string);
};

as i was implementinng it in the cpp file, i added #include to it, it seemed to be working, for example : as i was writing the "data." i got the "length()" appear by the intellisense, so i thought that it was working, but it wasn't. i got errors like:

syntax error : identifier 'string' overloaded member function not found in 'DeliTest'

this is one of the fucntions in the cpp file:

bool DeliTest::EofCheck(unsigned i, string data)
{

    if (i == data.length()-1)
        return 1;
    return 0;

}

am i supposed to be adding something to the header file??

+1  A: 

You should #include <string> and keep in mind that string is part of the standard library so it is in the std namespace. You can either add: using namespace std; at the top of your header or fully qualify the string class with std::string where you use it. You really should get in the habit of using a fully qualified class name especially in a header file.

Steven Behnke
-1 for suggesting adding "using namespace std;" in a *header* file. That can fly in a .cpp source file, but is inappropriate in a header. Damn, my vote limit is exceeded.
Michael Aaron Safyan
It's not a good idea to put `using namespace xxx;` in a header file.
Brian Neal
I said it was an option. I guess I should have said it was a bad option.
Steven Behnke
+6  A: 

In the header file you need:

#include <string>

strings live in the std:; namespace, so your functions should look like:

void DeliCheck(Stack*, std::string);

and although it is not wrong to pass strings by value, as you are doing, it is more common and better practice to pass them by const reference:

void DeliCheck(Stack*, const std::string & );
anon
@Neil, if I had votes remaining, I'd upvote you. Great answer.
Michael Aaron Safyan
+1  A: 

I assume you are trying to use the standard string class? If so you need to add:

#include <string>

at the top of your header file. And then also prefix your usage of string with std::; e.g.

void DeliCheck(Stack*, std::string);

You may also want to name your arguments for clarity, and you probably don't want to pass the strings by value:

void DeliCheck(Stack* s, const std::string& name);

Brian Neal
umm should i name the arguments in the header file too? cuz i thought it's enough to name them in the cpp file?
Nara
Naming the arguments is optional in the header file, but it can be useful to provide a description of what the arguments are.
Justin Ardini
Nara, consider reading a header file and finding a function like this: `void foo(char*, char*, char*);` Wouldn't you like to see the argument names to give you some idea of how to use the function?
Brian Neal
+1  A: 

Make sure to #include <string>, and because strings are in the std namespace, you should declare your strings as std::string in the header.

As a sidenote, make sure you do NOT declare using namespace std; in your header files. This would cause any other headers or c/cpp files that include this header to also use the std namespace. This is called polluting the namespace.

Justin Ardini
Thanks everyone for the super fast responses and tips :) problem solved !
Nara
Make sure to click the check mark on the left of the best answer to indicate that the problem has been solved.
Justin Ardini