错误 C4430 缺少类型说明符 - 假定为 int。注意:C++ 不支持 default-int 生成器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40594857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int Generator
提问by HeHacz
i have a problem with following code:
我有以下代码的问题:
Generator.h:
生成器.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 a error:
这是一个错误:
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
there is 6 more errors but i believe that they should disappeared after solving this problem.
还有 6 个错误,但我相信在解决这个问题后它们应该会消失。
this is the cpp file. I couldn't paste it on the first time. Generator.cpp
这是cpp文件。我第一次无法粘贴它。生成器.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;
}
回答by Vlad from Moscow
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
.
您必须<vector>
在所有引用的标题中包含标题std::vector
。
回答by M.M
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"
.
vector
不能用不完整的类型实例化。为了vector<BagObject> data;
在标题中有,标题也必须有#include "BagObject.h"
.
(This is in addition to the changes recommended in Vlad's answer)
(这是对弗拉德回答中推荐的更改的补充)
回答by ajeh
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
.
其他答案是正确的,但很神秘。用简单的英语,你的标题不知道BagObject
类。您包含BagObject.h
在 中.cpp
,但您应该将其包含在.h
.
It also does not know about vector
for the same reason.
vector
出于同样的原因,它也不知道。
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
.
我猜,您的印象是.cpp
必须使用#include
,但.h
没有使用。这是C++初学者的常见误区。头需要包括所有被引用类的声明,因此你需要提升您的包括.cpp
到您.h
。
Move two mentioned includes into the header and it will work.
将提到的两个包含移动到标题中,它将起作用。