Search code examples
c++typesvectormismatch

Type value mismatch error with vectors


I get the following error message when I try to run my program:

 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) ===

And here is my code for main leading up to the error causing line:

#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)
{

The header for enemy:

#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

And the constructor for enemy:

Enemy::Enemy(int Type)
{
    xsize=30;
    ysize=30;
    type=Type;
}

However it will run corrrectly if I substitute the type of my vector to an int. When the following line is commented out: std::vector enemyList; it compiles, however if it's there it doesn't when declaring an Enemy like this Enemy e(5);

it runs correctly

Updates: If i change the enemy header, and cpp to something like the following:

CPP:

#include "../include/Enemy.h"


Enemy::Enemy( )
{

}


Enemy::~Enemy()
{
    //dtor
}

Header

#ifndef ENEMY_H
#define ENEMY_H

class Enemy
{
    public:

        Enemy(  );
        ~Enemy();

    protected:
    private:

};



#endif // ENEMY_H

It still crashes with the same error, this means it must be something in main

FIX: For some reason when I declare it in the line above my enums it works, however if below it doesn't, I have no idea why. If someone could explain this please go ahead.


Solution

  • To have a vector of some value type, the type must have an available no-argument constructor, which yours doesn't (ie. that's what the error message is telling you). However, since I doubt you want to copy around Enemys, you should store them by pointer,ie.

    vector<Enemy*> enemies;
    for (int i = 0; i < NUM_ENEMIES; ++i)
        enemies.push_back(new Enemy(type));
    

    EDIT I just noticed you have the following declaration

    class Enemy...
    

    but you also declare an enum value

    enum render {... ,Enemy, ....
    

    I just compiled the following code and got a suspiciously similar error:

    #inlcude <vector>
    
    
    class A {};
    
    enum type {A, B, C};
    
    std::vector<A> As;
    
    int main(int argc, char** argv)
    {
      return 0;
    }
    

    So there's your problem. When it comes time to resolve the template, the compiler sees an enum value (which can be a template parameter, just like any other integral type), and assumes that's the one you meant. However, since no vector template matches (they all have a class as the first template parameter), it doesn't compile. Hence, your error.