Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
175 views
in Technique[技术] by (71.8m points)

c++ - error: expression must have a class type

I get the error: expression must have a class type Firstly, I don't understand why I am getting this error. I create and object and use it. in my main:

#include "Worker.h"

int main()
{

    Worker myWorker();
    myWorker.inputInfo();
    myWorker.displayPayBarGraph();

}

Worker.h

//Worker.h
//Definition of class Workers
//Member functions are defined in Worker.cpp


//Worker class defintion 

class Worker
{
public:
    Worker();               //constructor initializes worker type

    void inputInfo();           //attains worker information
    void displayPayBarGraph();  //prints a bar graph representation of the pay

private:

    int workerCode;     //worker type
    //PAY FOR EACH WORKER
    double code1pay;            //manager
    double code2pay;            //hourly workers
    double code3pay;            //commission workrs
    double code4pay;            //pieceworkers

    int hourlyWorkerPay(double, int);   //returns the pay of hourly workers
    int commissionPay(int);     //returns the commission workers pay
    int pieceWorkerPay(int, int);   //returns the pieceworkers pay
};
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Most vexing parse :

This line:

Worker myWorker();

declares a function taking no parameters and returning a Worker.

Simply declare your object with :

Worker myWorker;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...