Arduino C++ vector of int
Arduino C++ vector of int
我有一个名为Model的类,在ypur .h文件中我有这个:
private:
vector<int> memory(MEMORY_SIZE);
MEMORY_SIZE 是值为 10 的定义标头中的常量。
当我尝试编译时,我得到了这个错误代码
Model.h:33: error: ISO C++ forbids declaration of 'vector' with no type
Model.h:33: error: expected ';' before '<' token
我不知道为什么这样,我正在声明矢量的类型...
完整的标头代码:
/*
* 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_ */
和C++代码:
/*
* 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();
}
和错误:
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
这些是我的镜头,不包括矢量或您缺少命名空间std::
。编译器明确指出它不知道向量是什么。
更重要的是,您不会在C++中初始化这样的字段。你必须在构造函数中执行此操作:
#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;
}
相关文章:
- Strange behavior of std::vector<{QString,int*}>
- 访问 QMap of QMap<QString, QPair<int>>
- 为什么选择 g++ 给予者:"error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]"
- MinGW64和"conflicting declaration of C function int select(...)"
- unicode of char misused for int
- " invalid operands of types 'int' and 'int* const'" 来自功能的错误,来自书
- 混淆从"array of int[]"到"pointer to int"的显式衰减?
- 了解C++代码 - "Get the number of digits in an int"
- "a value of type " void (exeCallback::*)(int) " cannot be assigned to an entity of type " void (*)(
- Efficiency of int vs int[]
- Arduino C++ vector of int
- C/C++:在32位机器上的sizeof(short)、sizeof(int)、sizeof(long)、size of
- Pinvoke upon return of int, API
- 链表函数 C++ "which is of non-class type 'int'" ?
- int value of COM11?
- 返回 type of const int &
- Complement of unsigned int
- 如何使类型别名"a pointer to an array of const int"?
- 将 int** 转换为"pointer to a two-dimensional array of integers with fixed number of elements per column"
- Implementation of Long Atomic Int