C++ 错误:表达式必须具有类类型?类成员函数的问题
我是一个相当新手的程序员,在整理这个错误时遇到了一些麻烦。尝试在main末尾附近实现“formatReportLine()”函数时,会弹出以下错误: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() )
"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;
};