0

I need to implement a class for one of my assignment and one of the function in the class that has string as datatype doesn't work

my definition code is :

#include <string>  
class expression {
public:
    expression();
    void promptUser();
    int getNum1();
    int getNum2();
    int calculate();
    st::string str;
    string numToString(int num);
    string opToString();

private:
    int num1;
    int num2;
    char op;
};

And in my implementation file when I try to definite numTostring

string expression::numToString(int num) {
    string digit;
    ...

It says that the declaration is incompatible with the header file(my class definition)

I have no idea why because both the function heading are the same.

the header file of expression.cpp( the implementation file) are :

#include "expression1.h"
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cmath>

using namespace std;
Pang
  • 9,564
  • 146
  • 81
  • 122
mathilde
  • 9
  • 1
  • Try making a short example all in one file that demonstrates the problem. Only include enough code to demonstrate the issue. – Retired Ninja Nov 28 '14 at 00:35

3 Answers3

1

Your class uses the unqualified name string, but there is no string data type defined in any enclosing scopes. There's a std::string data type defined in namespace std. That's looks to be the type that you need:

std::string str;
std::string numToString(int num);
std::string opToString();

You can keep from having to type out std:: everywhere by specifying a using statement:

using std::string;

But you might not want to do that inside a header file, so stick with fully qualifying the type.

Community
  • 1
  • 1
David G
  • 94,763
  • 41
  • 167
  • 253
  • can i use include for string ? – mathilde Nov 28 '14 at 00:49
  • @mathilde What do you mean by that? – David G Nov 28 '14 at 00:51
  • @RichardHodges You're right, he shouldn't be using `using` inside headers. I'll update. – David G Nov 28 '14 at 00:52
  • it is in french.. but is basicly says the function definition is incompatible with the expression::numTostring etc – mathilde Nov 28 '14 at 01:01
  • @mathilde Make sure you are defining the function with the same return type and parameter declaration as the one in the expression1.h header file. – David G Nov 28 '14 at 01:05
  • @0x499602D2 it is exactly the same as in the header file it is only my string functions that won't work but i did everything you told me.. put the std::String before in the header file then #include and using namespace std for the implementation i still have the same message – mathilde Nov 28 '14 at 17:05
  • @0x499602D2 i actually moved #include and #include before #include "expression1.h" and the error message stopped i guess it was only that thank you so much for helping me everyone – mathilde Nov 28 '14 at 17:09
1

If you want to use , you need to refer to it with std::

For example, your expression class declares:

st::string str;
string numToString(int num);
string opToString();

Which should be:

std::string str; // you typed st:: instead of std::
std::string numToString(int num); // lack of std::
std::string opToString(); // lack of std::

If you dont use 2 files (cpp + h) to define and declare your class then you can add line

using namespace std;

just after your includes. This way you wont have to type std:: each time you try to refer to string and similar types. However, using this is often called a bad "beginner" practice.

If you do use cpp+h then just add std:: before every string type and add using namespace std; to your cpp file.

If you want to know more then read:
1. http://www.cplusplus.com/doc/tutorial/namespaces/
2. Why is "using namespace std" considered bad practice?
3. How do you properly use namespaces in C++?

Community
  • 1
  • 1
Patryk Krawczyk
  • 1,342
  • 1
  • 14
  • 25
0

You also need to move

#include "stdafx.h"

up so it is the first header included. The compiler ignores everything that comes before that magic line.

ScottMcP-MVP
  • 10,337
  • 2
  • 15
  • 15