2013-11-09 16 views
-6

我目前正在我的第一個項目在codeblocks,但是當我生成一個新的類它彈出一堆錯誤。codeblocks C++許多錯誤,同時使一個類

代碼:

#ifndef SERVICIO_H 
#define SERVICIO_H 
#include <iostream> 
#include <string> 
using namespace std; 

class Servicio 
{ 
public: 
    Servicio(); 
    virtual ~Servicio(); 
    int codigo Get[10]() { return [10]; } 
    void Set[10](int codigo val) { [10] = val; } 
    string nombre Get[10]() { return [10]; } 
    void Set[10](string nombre val) { [10] = val; } 
    float precio Get[10]() { return [10]; } 
    void Set[10](float precio val) { [10] = val; } 
    float comision Get[10]() { return [10]; } 
    void Set[10](float comision val) { [10] = val; } 
protected: 
private: 
    int codigo [10]; 
    string nombre [10]; 
    float precio [10]; 
    float comision [10]; 
} 

#endif // SERVICIO_H 

和錯誤日誌:

|12|error: expected ';' at end of member declaration| 
|12|error: 'Get' does not name a type| 
|13|error: expected ',' or '...' before 'val'| 
|13|error: declaration of 'Set' as array of functions| 
|13|error: expected ';' at end of member declaration| 
|14|error: expected ';' at end of member declaration| 
|14|error: 'Get' does not name a type| 
|15|error: expected ',' or '...' before 'val'| 
+1

1.閱讀錯誤信息。 2.評論一下東西。 –

+1

看起來你來自不同的語言與getter和setters。這甚至不是非常接近有效的代碼。 – 2013-11-09 15:37:18

+0

我是C++的新手,我以前只使用c和basic,但似乎真正的問題是我試圖依靠codeblocks的建議來使用getters和setters,所以我會停止這樣做,並且而是寫我自己的代碼。 – user2972213

回答

3

你之類的右括號後需要;

0

如果您可以使用C++ 11,那麼請考慮使用std::array。有關詳細信息,請參見this

#include <array> 
#include <iostream> 

class Servicio 
{ 
public: 
    Servicio() { } 
    virtual ~Servicio() { } 

我們不想成傳,因爲你只想get值。

std::array<int, 10> get_codigo() const { 
     return codigo; 
    } 

在這裏,您可以考慮將其分配給codigo之前做一些與value

void set_codigo(const std::array<int, 10>& value) { 
     codigo = value; 
    } 

protected: 
private: 
    std::array<int, 10> codigo; 
    std::array<std::string, 10> nombre; 
    std::array<float, 10> precio; 
    std::array<float, 10> comision; 
}; 

無論哪種方式這種編碼風格是繁瑣的,可能不是正確的做法。

+0

說實話,我提出的問題是,當我嘗試在其上輸入數組變量時,在類文件上產生了什麼,因此編譯器可能會感到困惑,並開始吹噓廢話。 – user2972213

0

什麼?這段代碼不像C++。在開始編碼之前,你確實需要閱讀一本書。 C++與你以前所知的任何語言都有很大的不同。這不僅僅是語法上的不同,概念是不同的。你不能僅僅使用你已經知道的來編寫C++,你將不得不做一些學習。

我想你不可能採取上述建議,所以這裏開始,至少是合法的代碼(但不是好的代碼)。

class Servicio 
{ 
public: 
    Servicio(); 
    int* GetCodigo() { return codigo; } 
... 
private: 
    int codigo [10]; 
};