0

I have a class named Model and in ypur .h file I have this:

private:
    vector<int> memory(MEMORY_SIZE);

MEMORY_SIZE is a const in a define header with value 10.

when I try compile I'm gettind this error code

Model.h:33: error: ISO C++ forbids declaration of 'vector' with no type
Model.h:33: error: expected ';' before '<' token

I don't know why this, I'm declaring the type of vector...


The complete header code:

/*
 * Model.h
 *
 *  Created on: Sep 13, 2012
 *      Author: ademar
 */


#ifndef MODEL_H_
#define MODEL_H_

#include "Config.h"
#include "Arduino.h"
#include <vector>

class Model {

    public:
        Model(int pin, char command[]);
        Model(int pin, int initialState, char command[]);
        bool isChanged(int currentState);
        char* getCommand(void);
        int getState();
        void setRange(int range);
        void usesMemory();

    private:
        int pin;
        int state;
        int range;
        long time;
        char* command;
        void updateTime();
        bool useMemory;
        std::vector<int> memory;
};

#endif /* MODEL_H_ */

And the C++ code:

/*
 * Model.cpp
 *
 *  Created on: Sep 13, 2012
 *      Author: ademar
 */

#include "Model.h"

Model::Model(int pin, char command[]) {
    *this = Model(pin,0,command);
}

Model::Model(int pin, int initialState, char command[]) {
    this->pin = pin;
    this->state = initialState;
    this->command = command;
    this->range = 1;
    this->useMemory = false;
    this->updateTime();
}

void Model::usesMemory(){
    this->useMemory = true;
}

void Model::setRange(int range){
    this->range = range;
}

bool Model::isChanged(int currentState) {
    if ((currentState >= (this->state + this->range) || currentState <= (this->state - this->range)) && ((this->time+WAIT_CHANGE)<millis())){
        this->state = currentState;
        updateTime();
        return true;
    }
    return false;
}

char* Model::getCommand(){
    return this->command;
}

int Model::getState(){
    return this->state;
}

void Model::updateTime(){
    this->time = millis();
}

And the error:

In file included from Honda.h:11,
             from Honda.cpp:8:
Model.h:33: error: ISO C++ forbids declaration of 'vector' with no type
Model.h:33: error: invalid use of '::'
Model.h:33: error: expected ';' before '<' token
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ademar111190
  • 14,215
  • 14
  • 85
  • 114

1 Answers1

1

These are my shots that vector is not included or you are missing namespace std::. The compiler explicitly points out that it does not know what vector is.

What is more, you don't initialize fields like this in C++. You have to do it in the constructor:

#include <vector>
#include <iostream>

#define MEMORY_SIZE 10

class Clazz {
    std::vector<int> memory;

    public:
        Clazz() : memory(MEMORY_SIZE){}
        int memory_size() {return memory.size();}
};

int main() {
    Clazz c;
    std::cout << c.memory_size() << std::endl;
    return 0;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kylo
  • 2,300
  • 1
  • 19
  • 24
  • not solve :/, do you make this wth arduino? I go paste all code on question. – ademar111190 Oct 01 '12 at 19:45
  • Nope, this is standart c++ and should work everywhere. Apparently it does not for andruino. Here is one of the first things I found in google: http://stackoverflow.com/questions/9986591/vectors-in-arduino – Kylo Oct 01 '12 at 20:02
  • Really in arduino it don't work, i'm using standarts arrays, thanks. only a detail in arduino i do an array follows: `int memory[MEMORY_SIZE];`. – ademar111190 Oct 02 '12 at 17:46