gpt4 book ai didi

c++ - vector 类型值不匹配错误

转载 作者:行者123 更新时间:2023-11-30 01:22:07 26 4
gpt4 key购买 nike

当我尝试运行我的程序时收到以下错误消息:

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

但是,如果我将 vector 的类型替换为 int,它将正确运行。当下面一行被注释掉时:std::vector enemyList;它编译,但是如果它在那里它不当像这样宣布敌人时敌人 e(5);

运行正常

更新:如果我更改敌人 header ,并将 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

它仍然因同样的错误而崩溃,这意味着它一定是在 main 中的东西

修复:出于某种原因,当我在我的枚举上方的行中声明它时它有效,但是如果在下面它不起作用,我不知道为什么。如果有人可以解释这一点,请继续。

最佳答案

对于某些功能,std::vector<T>要求 T可以默认构造。你的Enemy类不能默认构造,因此编译器会发出错误。为 Enemy 定义默认构造函数, 不要打电话 vector需要默认可构造性的函数,或更改 vector成为不同的类型。

鉴于 virtual在 Enemy 中使用,然后 std::vector<Enemy>永远不能接受派生类,再加上你使用令人作呕的全局变量、C 数组和其他糟糕的代码,我假设你不知道发生了什么。

A std::vector<Enemy>只能持有 Enemy .它不能包含 Enemy 的派生类或其他任何东西。它只能容纳 Enemy .如果你想要一个vector对于可能是各种派生类的事物,您必须使用某种程度的所有权指针。这意味着您必须解决谁拥有被指向的对象的问题,并且您必须进一步理解所有权的概念以及可用的智能指针。这是 C++ 的基本知识,没有它你就走不了多远。

关于c++ - vector 类型值不匹配错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16928648/

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