我在furniture.h中有这个:

#include <iostream>
#include <string>
using namespace std;

class Furniture {
public:
    Furniture();
    virtual ~Furniture();
    void setname(string name);
    void setprice(double price);
    int getprice();
    string getname();
private:
    string name;
    int price;
protected:
    static int NumberOfItems;
    int Id;

}


这在furniture.cpp中

#include "furniture.h"

void Furniture::setname(string name) {
    this->name = name;
}
string Furniture::getname()
{
    return this->name;
}
void Furniture::setprice(double price) {
    this->price = price;
}
int Furniture::getprice() {
    return this->price;
}

int main() {
    Furniture *model = new Furniture();
    model->setname("FinalDestiny");
    model->setprice(149.99);
    cout<<"Model name: "<<model->getname()<<" - price = "<<model->getprice();
}


但是我遇到了一些错误,例如:

错误1错误C2628:“家具”后跟“无效”是非法的(您是否忘记了“;”?)c:\ final \ facultate \ poo \ laborator 1 \ furniture.cpp 3 1 POO_lab

错误2错误C2556:'Furniture Furniture :: setname(std :: string)':重载函数仅与'void Furniture :: setname(std :: string)'的返回类型有所不同c:\ final \ facultate \ poo \ laborator 1 \ furniture.cpp 3 1 POO_lab

错误3错误C2371:'Furniture :: setname':重新定义;不同的基本类型c:\ final \ facultate \ poo \ laborator 1 \ furniture.cpp 3 1 POO_lab

错误5错误C2264:'Furniture :: setname':函数定义或声明错误;函数未调用c:\ final \ facultate \ poo \ laborator 1 \ furniture.cpp 19 1 POO_lab

我究竟做错了什么?

最佳答案

您的头文件中类定义的末尾缺少;

// ...snipped...

protected:
    static int NumberOfItems;
    int Id;

}; // <-- here

关于c++ - C++中的类问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9762757/

10-11 04:47