Reputation: 161
I have a problem with the following code:
Generator.h:
#pragma once
class Generator
{
public:
friend class BagObject;
Generator(void);
~Generator(void);
...
void generator(int);
private:
BagObject *object;
vector<BagObject> data; //Error c4430
};
And this is the error:
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
(There are 6 more errors but I believe that they should disappear after solving the above problem.)
Generator.cpp
#include "stdafx.h"
#include "Generator.h"
#include "BagObject.h"
#include <iostream>
#include <vector>
#include <ctime>
using namespace std;
Generator::Generator(void)
{
srand(time(NULL));
}
Generator::~Generator(void)
{
data.clear();
}
void Generator::generator(int ld)
{
for (int i = 0; i<ld; i++)
{
object = new BagObject(rand(),rand(),i);
data.push_back(object);
}
}
int main()
{
Generator *g = new Generator;
g->generator(10);
return 0;
}
Upvotes: 15
Views: 77331
Reputation: 108
I know your problem is solved but in My case the same error was caused due to cyclic includes (i.e. I had accidentally included the .h file in one of the .h file included in it)
TextureManager.h (The file with the error)
// This is TextureManager.h
#pragma once
#include "Game.h"
#include "GameObject.h"
/*
texture manager class
*/
GameObject.h
// This is GameObject.h
#pragma once
#include "game.h"
#include "TexutureManager.h" // Accidental
/*
*/
I thought it may be worth noting this is also one of the ways to get this error.
Upvotes: 1
Reputation: 2784
Other answers are correct, but cryptic. In plain English, your header does not know about BagObject
class. You included BagObject.h
in the .cpp
, but you should have included it in the .h
.
It also does not know about vector
for the same reason.
I am guessing, you were under impression that .cpp
had to use #include
, but .h
did not. This is a common misunderstanding of beginners in C++. Headers need to include all referenced class declarations, hence you need to elevate your includes from .cpp
into your .h
.
Move two mentioned includes into the header and it will work.
Upvotes: 11
Reputation: 310980
Either you forgot to include header
#include <vector>
or forgot to write directive
using namespace std;
In any case it would be better to write
#include <vector>
//...
std::vector<BagObject> data;
^^^^^
You have to include the header <vector>
in all headers where there is a reference to std::vector
.
Upvotes: 17
Reputation: 141576
vector
may not be instantiated with an incomplete type. In order to have vector<BagObject> data;
in the header, the header must also have #include "BagObject.h"
.
(This is in addition to the changes recommended in Vlad's answer)
Upvotes: 3