当我尝试运行程序时收到以下错误消息:
main.cpp|44|error: type/value mismatch at argument 1 in template
parameter list for 'template<class _Tp, class _Alloc> class
std::vector' main.cpp|44|error: expected a type, got '(render)3u'
main.cpp|44|error: template argument 2 is invalid main.cpp|44|error:
invalid type in declaration before ';' token
=== Build finished: 4 errors, 0 warnings (0 minutes, 0 seconds) ===
这是我的主要导致错误导致行的代码:
#include<stdlib.h>
#include<windows.h>
#include<GL/glut.h>
#include<GL/freeglut.h>
#include<iostream>
#include <vector>
#include "include/Block.h"
#include <string>
#include <sstream>
#include "include/TextBlock.h"
#include "include/Enemy.h"
string text;
stringstream ss;
enum render {Normal,SelectRangeText,SelectSText,Enemy,EnemyTypeSelection};
enum render state;
enum type { Foreground, Background, Midground };
enum type selected;
enum types { Blocks, Enemies, Text };
enum types special;
string names[4] = { "grass" , "smallGrassBlock" , "dirt" , "sand" };
void createEnemy(int,int);
void addEnemyWaypoint(int,int);
void addToList(vector<Block> &list,int x,int y);
void placeText(int x,int y);
void initTextures();
GLint GetTexture(string file);
void IdleFunction();
void removeBlock(int x,int y);
int xOffset,yOffset,multiuse = 0;
using namespace std;
string to_string(int number);
void placeBlock(int x,int y);
void drawBitmapText(char *string, float x, float y, float z);
void reshape(int w, int h);
void render(void);
void keyboard(unsigned char c,int x,int y);
void mouse(int button,int state, int x, int y);
void arrows(int key, int x, int y );
std::vector <Block> backBlockList;
std::vector <TextBlock> textBlockList;
std::vector <Block> midBlockList;
std::vector <Block> foreBlockList;
std::vector <Enemy> enemyList;//error occurs here
GLuint textures[1];
unsigned int screenXSize=800;
unsigned int screenYSize=800;
unsigned int selectedBlockType = 1;
int main(int argc, char ** argv)
{
敌人的标题:
#ifndef ENEMY_H
#define ENEMY_H
#include<GL/freeglut.h>
#include <vector>
class Enemy
{
public:
Enemy(int Type );
virtual ~Enemy();
void render(int,int,GLuint);
void addWaypoint(int,int);
protected:
private:
int type;
std::vector <int> X, Y;
int xsize,ysize;
};
#endif // ENEMY_H
敌人的构造函数:
Enemy::Enemy(int Type)
{
xsize=30;
ysize=30;
type=Type;
}
但是,如果我将向量的类型替换为int,它将正确运行。 当以下行注释掉时:std :: vector enemyList;它编译,但如果它在那里它没有 当宣布像这样的敌人 敌人e(5);
它正确运行
更新: 如果我改变了敌人的头部,并且cpp改为如下:
CPP:
#include "../include/Enemy.h"
Enemy::Enemy( )
{
}
Enemy::~Enemy()
{
//dtor
}
标题
#ifndef ENEMY_H
#define ENEMY_H
class Enemy
{
public:
Enemy( );
~Enemy();
protected:
private:
};
#endif // ENEMY_H
它仍然会因同样的错误而崩溃,这意味着它必须是主要的
FIX: 出于某种原因,当我在我的枚举上面的行中声明它时,它可以工作,但是如果它不在下面,我不知道为什么。如果有人可以解释这一点,请继续。
答案 0 :(得分:3)
对于某些功能,std::vector<T>
要求T
是默认构造的。您的Enemy
类不能默认构造,因此编译器会发出错误。定义Enemy
的默认构造函数,不要调用需要默认构造性的vector
函数,或者将vector
更改为其他类型。
鉴于在敌人中使用virtual
,并且std::vector<Enemy>
永远不会接受派生类,加上你使用恶心的全局变量,C数组和其他可怕的代码,我会假设你不知道发生了什么事。
std::vector<Enemy>
只能持有Enemy
。它不能保存Enemy
或其他任何内容的派生类。它只能保留Enemy
。如果您希望有vector
个可能是各种派生类的东西,则必须使用某种程度的所有权指针。这意味着您必须解决谁拥有所指向对象的问题,并且您必须了解所有权的概念以及可用的智能指针。这对于C ++来说是必不可少的知识,如果没有它,你就无法走得太远。
答案 1 :(得分:2)
要拥有某个值类型的vector
,该类型必须具有可用的无参数构造函数,而您的函数没有(即错误消息告诉您的内容)。但是,因为我怀疑你想复制Enemy
,你应该用指针存储它们,即。
vector<Enemy*> enemies;
for (int i = 0; i < NUM_ENEMIES; ++i)
enemies.push_back(new Enemy(type));
编辑我刚注意到您有以下声明
class Enemy...
但您也声明了枚举值
enum render {... ,Enemy, ....
我刚刚编译了以下代码并遇到了可疑的类似错误:
#inlcude <vector>
class A {};
enum type {A, B, C};
std::vector<A> As;
int main(int argc, char** argv)
{
return 0;
}
所以这是你的问题。当需要解析模板时,编译器会看到一个枚举值(可以是模板参数,就像任何其他整数类型一样),并假设它是您所指的那个。但是,由于没有矢量模板匹配(它们都有一个类作为第一个模板参数),因此它不会编译。因此,你的错误。