-7

I'm currently making my first project in codeblocks, but when i generate a new class it pops up a bunch of errors.

Code:

#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

And the error log:

|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. Read the error messages. 2. Comment stuff out. – Ed Heal Nov 09 '13 at 15:34
  • 1
    It seems you've come from a different language with getters and setters. This isn't even remotely close to valid code. –  Nov 09 '13 at 15:37
  • I'm a newbie to c++, i only used c and basic before, but it seems that the real problem is that i'm trying to rely on codeblocks's suggestion to use getters and setters, so i'll just stop doing that and instead write my own code. – user2972213 Nov 09 '13 at 16:07
  • Lacks the minimal understanding. – László Papp Mar 01 '14 at 01:23

3 Answers3

3

You need a ; after the closing bracket of the class.

Eutherpy
  • 4,471
  • 7
  • 40
  • 64
0

If you can use C++11, then consider using std::array. See this for details.

#include <array>
#include <iostream>

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

We don't want to return by reference since you only want to get the value.

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

Here you can consider doing something with value before assigning it to codigo.

    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;
};

Either way this style of coding is cumbersome and probably not the correct approach.

Community
  • 1
  • 1
  • To be honest, what i put on my question is what's generated on the class file whenever i try to input array variables on it, so the compiler probably gets confused and starts spouting nonsense. – user2972213 Nov 09 '13 at 15:54
0

What? This code is nothing like C++. You really need to read a book before you start coding. C++ is very different from whatever language you knew before. It's not just syntactically different, the concepts are different. You are not going to be able to code C++ just by using what you know already, you are going to have to do some studying.

I guess you're not likely to take the advice above, so here's start, it is at least legal code (but not good code though).

class Servicio
{
public:
    Servicio();
    int* GetCodigo() { return codigo; }
...
private:
    int codigo [10];
};
john
  • 85,011
  • 4
  • 57
  • 81