Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int Generator
Asked Answered
C

4

14

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;
}
Constrain answered 14/11, 2016 at 17:51 Comment(1)
I am just guessing but didnt you forget std::vector or include the BagObject header?Anthocyanin
A
16

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.

Afro answered 14/11, 2016 at 17:54 Comment(15)
Looking at #pragma once statement you dont want to recommend using namespace std as this is most probably a header fileAnthocyanin
@AdrianLis Where do you have seen that I recommend to use it?Afro
maybe recommend wasnt the best choice of words but you did suggest maybe he forgot to write using namespace std, and doing so isn't a good option in this case because of the header.Anthocyanin
@AdrianLis I see that he uses unqualified names so I concluded that he already uses the directive.Afro
Whatever you did or did not recommend, I recommend not writing using namespace std; in a header file.Mezzo
@KeithThompson The question is not about using or not using the directive. The question is about why the compiler error occurred. And moreover he is a beginner and maybe learn the language by reading some book where all examples have this directive. So when someone will ask a question about this directive you can give your advice.:)Afro
Thank you for your input. If I were answering the question, I would have posted an answer. I posted a comment, in response to other comments. If you don't want me giving my advice, I'm afraid that's just too bad.Mezzo
When i Write std::vector<BagObject> data; i got one more error Error C2039 'vector': is not a member of 'std'Constrain
@Constrain As I wrote in my answer you have to include header vector like #include <vector>. The class vector is declared in this header.Afro
I include #include <vector> and #include "BagObject.h" still same error.Constrain
@Constrain If you included <vector> then this error can not occur. It means that you included the header in some other compilation unit instead of this one.Afro
@HeHacz: Your header file Generator.h refers to vector. Do you have #include <vector> in Generator.h? (In addition to that, you'll need to either refer to std::vector rather than just vector or add using namespace std;.)Mezzo
@Constrain Check all headers where there is a reference to std::vector and include there the header.Afro
Ok i create a simple program that use vector and there is a problem with my dll files so i need probably to reinstal C++ redistributable.Constrain
@Constrain Maybe. We know nothing about your dll.:)Afro
I
10

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.

Idolater answered 28/3, 2018 at 14:51 Comment(1)
Sir, thank you from the bottom of my heart, you solved a problem that no one in the Unreal Engine community was able to resolve decently.. Someone was saying "You can't forward declare enums", someone else "You can't use that macro", I was able to do both just including the file where the enum was declared in the .h file (In unreal almost everything is included in the .cpp)Yodel
T
3

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)

Throw answered 14/11, 2016 at 20:33 Comment(0)
D
1

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.

Depone answered 27/7, 2022 at 6:35 Comment(1)
Thanks for your comment, this saved me a lot of pain!Aircondition

© 2022 - 2024 — McMap. All rights reserved.