When hiding header and implementation files, why can't a string be a parameter to a function

I just have a class that takes a string var to a function and outputs the string. For some reason, the compiler is giving me errors. Everything works fine when I use a char var with the function but I get errors when I use strings...here is the program

error:'string' has not been declared in header file
error: no matching function for call to 'Print::PrintString(std::string&) in main file

Main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "Print.h"
#include <string>
#include <iostream>
using namespace std;

int main()
{
    string text;
    Print print;

    cout<<"Enter a word: ";
    cin>>text;
    cout<<endl;

    print.PrintString(text);

    return 0;
}


Header File
1
2
3
4
5
6
7
8
9
10
11
#ifndef PRINT_H
#define PRINT_H

class Print
{
    public:
        Print();
        void PrintString(string);
};

#endif // PRINT_H 


Cpp File
1
2
3
4
5
6
7
8
9
10
11
12
13
#include "Print.h"
#include <string>
#include <iostream>
using namespace std;

Print::Print()
{
}

void Print::PrintString(string text)
{
    cout<<text<<endl;
}


Thanks for any insight
Last edited on
It is because the header file does not know what a 'string' is. It can't see the #include <string> in the .cpp file or in main. You will need to include the string library in the header file then use std::string for the variable (It is usually a bad idea to have using namespace std in a header as then all other files are forced to use it).
Last edited on
Thanks a bunch! Was stuck on this for a while...
Topic archived. No new replies allowed.