gpt4 book ai didi

c++ - int 的 Arduino C++ vector

转载 作者:行者123 更新时间:2023-11-28 07:56:09 25 4
gpt4 key购买 nike

我有一个名为 Model 的类,在 ypur .h 文件中我有这个:

private:
vector<int> memory(MEMORY_SIZE);

MEMORY_SIZE 是定义 header 中的常量,值为 10。

当我尝试编译时,我发现了这个错误代码

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

我不知道为什么会这样,我正在声明 vector 的类型...


完整的头部代码:

/*
* 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

最佳答案

这些是我的截图,vector 不包括在内,或者您缺少命名空间 std::。编译器明确指出它不知道 vector 是什么。

此外,您不会在 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;
}

关于c++ - int 的 Arduino C++ vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12679399/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com