简体   繁体   中英

class issue in C++

I have this in 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;

}

and this in 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();
}

But I get some errors like:

Error 1 error C2628: 'Furniture' followed by 'void' is illegal (did you forget a ';'?) c:\\final\\facultate\\poo\\laborator 1\\furniture.cpp 3 1 POO_lab

Error 2 error C2556: 'Furniture Furniture::setname(std::string)' : overloaded function differs only by return type from 'void Furniture::setname(std::string)' c:\\final\\facultate\\poo\\laborator 1\\furniture.cpp 3 1 POO_lab

Error 3 error C2371: 'Furniture::setname' : redefinition; different basic types c:\\final\\facultate\\poo\\laborator 1\\furniture.cpp 3 1 POO_lab

Error 5 error C2264: 'Furniture::setname' : error in function definition or declaration; function not called c:\\final\\facultate\\poo\\laborator 1\\furniture.cpp 19 1 POO_lab

What am I doing wrong?

You are missing a ; at the end of the class definition in your header file.

// ...snipped...

protected:
    static int NumberOfItems;
    int Id;

}; // <-- here

You've forgotten a semicolon at the end of your class definition.

// ...
protected:
    static int NumberOfItems;
    int Id;
}; // <--

I hate that about C++ :)

Two things;

  • You're not ending your class definition with a ; , you need one at the end of furniture.h.

  • You've declared that there's a constructor and destructor, but neither is implemented in your .cpp file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM