Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 错误:表达式必须具有类类型?类成员函数的问题_C++_Expression - Fatal编程技术网

C++ 错误:表达式必须具有类类型?类成员函数的问题

C++ 错误:表达式必须具有类类型?类成员函数的问题,c++,expression,C++,Expression,我是一个相当新手的程序员,在整理这个错误时遇到了一些麻烦。尝试在main末尾附近实现“formatReportLine()”函数时,会弹出以下错误: "2 IntelliSense: expression must have class type" main.cpp int main() { const string FILENAME("books.txt"); ifstream input(FILENAME); if( input.good() )

我是一个相当新手的程序员,在整理这个错误时遇到了一些麻烦。尝试在main末尾附近实现“formatReportLine()”函数时,会弹出以下错误:

"2  IntelliSense: expression must have class type"
main.cpp

    int main()
{
    const string FILENAME("books.txt");

    ifstream input(FILENAME);

    if( input.good() )
    {
        while( !input.eof() )
        {
            string name;
            int type;
            int pages;
            float ounces;
            getline( input, name );
            input >> type >> pages >> ounces;
            input.ignore(INT_MAX, '\n'); 


            Book newBook( const string& name, Type type, int pages, float ounces );

            cout << newBook.formatReportLine() << endl; //Difficulty with this line

        }
    }
    else
    {
        cout << "File not found: " << FILENAME << endl;
    }

    return 0;
}

任何帮助都将不胜感激。谢谢。

新书(名称、类型、页数、盎司)不幸的是,当我尝试这样做(用它替换所有的newbook实例)时,我得到了错误“2 IntelliSense:没有构造函数的实例”Book::Book”匹配参数列表参数类型是:(std::string,int,int,float)
Book newbook(名称,静态类型,页数,盎司)//Constructor
Book::Book( const string& name, Type type, int pages, float ounces )
{
    bName = static_cast<string>(name);
    bType = type;
    bPages = pages;
    bOunces = ounces;
    cout << "The constructor is running" << endl;
}

//Default Constructor
Book::Book() {
    bName = "";
    bType = UNKNOWN;
    bPages = 0;
    bOunces = 0.0f;
}

float Book::getWeightLbs()
{
    const float OUNCES = 16.0f;
    return bOunces / OUNCES;
}

string Book::getTypeName()
{
    return TYPE_WORDS[bType];
}

string Book::formatReportLine() 
{
    stringstream reportLine;
    reportLine << Book::getName() << setw(5) << "| Type: " << Book::getTypeName() << setw(5) << "Pages: " 
    << Book::getPages() << setw(5) << "Weight (lbs): " << Book::getWeightLbs();
    string newReportLine;
    reportLine >> newReportLine;
    return newReportLine;
}
using namespace std;

enum Type
{
    UNKNOWN = -1,
   PAPERBACK,
    HARDBACK
};

const string TYPE_WORDS[] = { "Paperback", "Hardback" };

class Book
{
public:
    Book();
    //constructor
    Book( const string& name, Type type, int pages, float ounces );
    //destructor
    ~Book(){};

    string formatReportLine();
    float getWeightLbs();   
    string getTypeName();
    //accessors
    string getName(){ return bName; };
    Type getType(){ return bType; };
    int getPages(){ return bPages; };
    float getOunces(){ return bOunces; };

private:
    string bName;  
    Type bType;  
    int bPages;  
    float bOunces;  
};