codeblocks c ++在创建类时会出现很多错误

时间:2013-11-09 15:32:57

标签: c++ arrays

我目前正在使用codeblocks创建我的第一个项目,但是当我生成一个新类时,它会弹出一堆错误。

代码:

#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'|

3 个答案:

答案 0 :(得分:3)

在课程结束时你需要;

答案 1 :(得分: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;
};

无论哪种方式,这种编码方式都很麻烦,而且可能不是正确的方法。

答案 2 :(得分:0)

什么?这段代码与C ++完全不同。在开始编码之前,你真的需要读一本书。 C ++与您之前所知的任何语言都有很大的不同。它不仅仅在语法上不同,概念也不同。你不能仅仅通过使用你已经知道的东西来编写C ++代码,你将不得不做一些学习。

我猜你不太可能接受上面的建议,所以从一开始,它至少是合法代码(但不是好代码)。

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