Header file: string is not declared

This is my first shot at using a header file and linking it to other projects, so bare with me, this whole thing is probably full of a ton of issues.. But I cannot make it past this one particular issue, once I do, I will try work out the other things... But right now I keep getting 'string' has no been declared.

Here's project..

Header file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef LAB2HEADER_H_INCLUDED
#define LAB2HEADER_H_INCLUDED
#include <string>
    
class employee
{

public:
    employee(); /// constructor
    void collectName(string name, string lastName); /// Add First+Last to get Full Name
    void collectSalary(int); /// Collect Salary
    void displayData();


private:
    string name;
    string lastName;
    int salary;
};


#endif // LAB2HEADER_H_INCLUDED 




.cpp file for functions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <string>
#include "Lab2header.h"

using namespace std;

employee::employee()
{
    name = i;
    lastName =i;
    salary = 0;
};

void employee::collectName (string name, string lastName);
{

   cout << "\nEnter employee #1's first name : ";
   cin >> dynamic[0].name;
   cout << "\n\nEnter employee #1's last name : ";
   cin >> dynamic[0].lastName;

   cout << "\n\nEnter employee #2's first name: ";
   cin >> dynamic[1].name;
   cout << "\nEnter employee #2's last name: ";
   cin >> dynamic[1].lastName;
};

void employee::collectSalary (int salary)
{
    cout << "\n\nWhat is employee #1's salary : ";
    cin >> dynamic[0].salary;
    cout << "\n\nWhat is employee #2's salary : ";
    cin >> dynamic[1].salary;
};

void employee::displayData ()
{
    cout << dynamic[0].name << " " << dynamic[0].lastName << " salary is " << dynamic[0].salary << endl;
    cout << dynamic[1].name << " " << dynamic[1].lastName << " salary is " << dynamic[1].salary << endl;
};



main .cpp :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include "Lab2header.h"
#include <string>

using namespace std;

int main()
{
    employee t;
    employee *dynamic = new employee[1];
    t.collectName();
    t.collectSalary();
    t.displayData();
    return 0;
}
try std::string
Okay.. I fixed the string issue, I think what the problem was is that I didn't have the project set up correctly.. I'm still having issues though, so any further help would be appreciated..

Current set up:

header:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <string>
#ifndef string_H_INCLUDED
#define string_H_INCLUDED

class employee
{


public:
    employee(); /// constructor
    void collectName(std::string name, std::string lastName); /// Add First+Last to get Full Name
    void collectSalary(int); /// Collect Salary
    void displayData();


private:
     employee *dynamic = new employee[1];
    std::string name;
    std::string lastName;
    int salary;
};


#endif // LAB2HEADER_H_INCLUDED 


functions:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <string>
#include "string.h"

using namespace std;

employee::employee()
{
    string name, lastName = "string";
    int salary = 0;
};

void employee::collectName (string name, string lastName)
{

   cout << "\nEnter employee #1's first name : ";
   cin >> dynamic[0].name;
   cout << "\n\nEnter employee #1's last name : ";
   cin >> dynamic[0].lastName;

   cout << "\n\nEnter employee #2's first name: ";
   cin >> dynamic[1].name;
   cout << "\nEnter employee #2's last name: ";
   cin >> dynamic[1].lastName;
};

void employee::collectSalary (int salary)
{
    cout << "\n\nWhat is employee #1's salary : ";
    cin >> dynamic[0].salary;
    cout << "\n\nWhat is employee #2's salary : ";
    cin >> dynamic[1].salary;
};

void employee::displayData ()
{
    cout << dynamic[0].name << " " << dynamic[0].lastName << " salary is " << dynamic[0].salary << endl;
    cout << dynamic[1].name << " " << dynamic[1].lastName << " salary is " << dynamic[1].salary << endl;
};


main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include "string.h"
#include <string>

using namespace std;


int main()
{
    employee t;
    t.collectName();
    t.collectSalary();
    t.displayData();
    return 0;
}



Now my error is in main it says :

no matching function call to 'employee::collectName()'
no matching function call to 'employee::collectSalary()'

Any help with these would be nice ^_^
Last edited on
You have defined collectName() to take 2 arguments, and collectSalary() to take 1 argument. How many arguments are you actually passing in when you call those methods?

EDIT: And, now that I look at those function definitions, perhaps you ought to consider why you want those functions to take those arguments, and what you are or aren't doing with them.
Last edited on
Something like this, perhaps:

employee.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef EMPLAYEE_H_INCLUDED
#define EMPLOYEE_H_INCLUDED

#include <string>

class employee
{
    public:
        employee() = default ;
        employee( std::string first_name, std::string last_name, double salary ) ;

        std::string full_name() const ;
        int salary() const ;
        void display() const ;

    private:
        std::string first_name ;
        std::string last_name ;
        double base_salary = 0 ;
};


#endif // EMPLOYEE_H_INCLUDED 


employee.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "employee.h"
#include <iostream>

employee::employee( std::string fname, std::string lname, double sal ) :
         first_name(fname), last_name(lname), base_salary(sal) {}

std::string employee::full_name() const { return last_name + ", " + first_name ; }

int employee::salary() const
{
    double amount = base_salary ;
    // add allowances
    return amount ;
}

void employee::display() const
{ std::cout << "name: " << full_name() << "  salary: " << salary() << '\n' ; }


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include "employee.h"

static employee& update_from_stdin( employee& e )
{
    std::string fname ;
    std::cout << "first name: " && std::cin >> fname ;

    std::string lname ;
    std::cout << "last name: " && std::cin >> lname ;

    double sal ;
    std::cout << "salary: " && std::cin >> sal ;

    return e = employee( fname, lname, sal ) ;
}

int main()
{
    const int NEMPLOYEES = 5 ;
    employee emps[NEMPLOYEES] ;

    // for each employee in array 'emps'
    for( employee& e : emps ) update_from_stdin(e) ;

    for( const employee& e : emps ) e.display() ;

}
Thank you all for the advice. I mixed in the above and I achieved what I was aiming for.. But can anyone explain to me how line 5 in employee.cpp works? I'm assuming : links the class variables with the variables collected in main, but how does that work? Also, I'm confused with how employee& update_from_stdin works and the for loops (lines 24 and 26 in main). If anyone can explain those, or point me to a tutorial/page that might further shed light onto them I'd very much appreciate it..
Last edited on
line 5 in employee.cpp :
Initialize class members from constructor arguments by using a member initializer list. This method uses direct initialization, which is more efficient than using assignment operators inside the constructor body.
https://msdn.microsoft.com/en-us/library/s16xw1a8.aspx#member_lists


how employee& update_from_stdin works:

the long form of return e = employee( fname, lname, sal ) ; is:

1
2
3
4
5
6
7
8
9
10
11
12
// step 1. create a temporary employee object initialised with fname, lname, sal  
employee temporary_object( fname, lname, sal ) ; 

// step 2 : assign this temporary object to 'e' (reference to the employee object in the array)
//          'e' gets modified to become a copy of the temporary_object
//          in practice, the temporary object is move assigned rather than copy assigned;
//              you may want to ignore this nicety for now
e = temporary_object ; // e = std::move(temporary_object) ;

// step 3. return (reference) to 'e' (the object that was passed to the function, and modified in it)
//             note: this result is ignored in main () where update_from_stdin(e) is a discarded-value expression
return e ;


for loops (lines 24 and 26 in main):
A range for statement allows you to iterate through a "range" ... All standard containers can be used as a range, as can a std::string, an initializer list, an array, ....
http://www.stroustrup.com/C++11FAQ.html#for



Also note the const qualifier on the member functions full_name, salary and display
Declaring a member function with the const keyword specifies that the function is a "read-only" function that does not modify the object for which it is called .... To declare a constant member function, place the const keyword after the closing parenthesis of the argument list. The const keyword is required in both the declaration and the definition.
https://msdn.microsoft.com/en-us/library/07x6b05d.aspx
Topic archived. No new replies allowed.