2012-03-18 80 views
0

我有這樣的furniture.h:類問題在C++

#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 :'Furniture'後跟'void'是非法的(你忘了';'?)c:\ final \ facultate \ poo \ laborator 1 \ furniture.cpp 3 1 POO_lab

Error 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':redefinition;不同的基本類型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

我在做什麼錯?

+2

錯誤1錯誤C2628: '傢俱',然後 '無效' 是非法的**(你忘了一個 ';'?)** – 2012-03-18 22:10:02

+2

RTFE:閱讀...錯誤。 – adelphus 2012-03-18 22:26:48

回答

7

您在頭文件中類定義的末尾缺少;

// ...snipped... 

protected: 
    static int NumberOfItems; 
    int Id; 

}; // <-- here 
+0

是啊thx !!!!!!!! – FinalDestiny 2012-03-18 22:13:25

5

您在班級定義的末尾忘了分號。

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

我恨關於C++ :)

+0

@JerryCoffin:謝謝,修正。你知道,你總是可以編輯它;) – Ryan 2012-03-18 22:36:38

3

兩件事情;

  • 你不是有;結束你的類定義,你需要一個在furniture.h結束。

  • 你已經聲明有一個構造函數和析構函數,但都沒有在你的.cpp文件中實現。