Hi,
I am stuck trying to figure out how to include a class within a class and how to properly call members of a class from the main(). I cannot figure out what these error messages mean. I would greatly appreciate and insight into my dilemma. Thank You, C.D.

Enclosed is the last attempt I made with the project.

// PEmployee header
#include <string>
#include "Lab10.h"
using namespace std ;

class PEmployee
{
public:
    PEmployee() ;
    PEmployee(string employee_name, double initial_salary) ;
    void set_salary () const ;
    double set_salary(double new_salary) const ;
    string get_name() const ;
    void read_salary() ; 

private:
    person person_data ;
    double salary ;
    string name ;
} ;

// person header
#ifndef Lab10
#define Lab10
#include <string>
using namespace std ;

class person
{
public:
    person()    ;
    person( string pname, int page ) ;
    string get_name() const ;
    int get_age() const ;
    void read_name() ;
    void read_age() ;

private:
    string name ;
    int age ;
} ;
#endif

// person definitions
#include <string>
#include "Lab10.h"
using namespace std ;

class PEmployee
{
public:
    PEmployee() ;
    PEmployee(string employee_name, double initial_salary) ;
    void set_salary () const ;
    double set_salary(double new_salary) const ;
    string get_name() const ;
    void read_salary() ; 

private:
    person person_data ;
    double salary ;
    string name ;
} ;

// PEmployee definitions
#include <iostream>
#include <string>
#include "Lab10.h"
#include "PEmployee.h"
using namespace std ;

PEmployee :: PEmployee()
{
    salary = 0 ;
}

PEmployee ::PEmployee(string employee_name, double initial_salary)
{
        salary = initial_salary ;
        name = employee_name ;
}

double PEmployee :: set_salary(double new_salary) const
{
    double salary = new_salary ;
    return salary ;
}

string PEmployee :: get_name() const
{
    return name ;
}

void person :: read_name()
{
    person next ;
    next.read_name ;
    string employee_name = next.name ;
}

void PEmployee :: read_salary() 
{
    double new_salary = 0 ;
    cout << "Please enter the salary for the person " ;
    cin >> new_salary ;
}

// attempt to call member function
void main()
{
    person next ;
    cout << next.read_name ;
    system("PAUSE") ;
}

Dani AI

Generated

is spot on about the missing parentheses: writing obj.method names the member function; writing obj.method() actually calls it. Without (), MSVC treats it as a pointer-to-member, which matches error C3867. If you really needed a pointer-to-member, you would take &Type::method and invoke it with .* or ->*, but that is not what you want here. Also note you cannot stream a void return into <<. See Compiler Error C3867 and a refresher on pointers to members.

Your name-reading routine should mutate the current object, not recurse or assign to a local. A minimal pattern that avoids leftover newlines is:

struct Person {
  void read_name() { std::getline(std::cin >> std::ws, name_); }
  const std::string& name() const { return name_; }
private:
  std::string name_;
};

// usage
Person p;
p.read_name();
std::cout << p.name() << '\n';

Building on ’s point: do not shadow your data members in setters, and do not mark a setter const (a const member function promises not to modify the object). A simple, clear approach is:

struct Employee {
  explicit Employee(std::string n = {}, double s = 0.0)
    : name_(std::move(n)), salary_(s) {}
  void set_salary(double s) { salary_ = s; }
  double salary() const { return salary_; }
private:
  std::string name_;
  double salary_;
};

A few hygiene fixes that will save you pain:

  • Use int main() (not void main()).
  • Do not put using namespace std; in headers.
  • Give each class one declaration (in a header with include guards) and one definition (in a .cpp). Duplicate class definitions across files violate the One Definition Rule.

Recommended Answers

All 3 Replies

Hi these are the errors I get:

Error 1 error C3867: 'person::read_name': function call missing argument list; use '&person::read_name' to create a pointer to member c:\users\admin\desktop\lab 10\lab 10\pemployee.cpp 63 1 Lab 10

Error 2 error C3867: 'person::read_name': function call missing argument list; use '&person::read_name' to create a pointer to member c:\users\admin\desktop\lab 10\lab 10\pemployee.cpp 91 1 Lab 10

When you call a function, you need a pair of parentheses. Thus it should next.read_name(), not next.read_name.

Also your read_name is infinitely recursive and can't possibly work. And all it does (other than overflowing the stack) is to write to a local variable that it then never uses - that can't be right.

Another problem is that read_name's return type is void, so you can't use it as an operand to << (or to any other operator or function).

In this code:

double PEmployee :: set_salary(double new_salary) const

{
    double salary = new_salary ;
    return salary ;

}

You are creating a local variable "salary" which will override the class member salary. IE, it won't set the instance member like you think. Remove the "double" declaration. There may be other, similar issues, but I don't have time right now to find them for you. :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.