C++当我使用标头时,函数会抛出错误,但如果我在源代码中定义它,则不会抛出错误?
当我尝试使用多个文件时,我在编译函数时遇到了一个奇怪的问题。我将其归结为这个简单的示例:假设我想找到整数向量的总和。如果我尝试编译以下代码,它会按预期工作:
#include <vector>
#include <iostream>
using namespace std;
int VectorSum (const vector<int>& values)
{
int S = 0;
for (int t=0; t < values.size(); t++)
{
S += values[t];
}
return S;
}
int main()
{
vector<int> values;
values.push_back(-100);
values.push_back(75);
values.push_back(75);
cout << "Total = " << VectorSum(values) << endl << endl;
cin.ignore(1, '\n');
return 0;
}
但是,如果我尝试使用头文件,它会崩溃(在 Windows XP 的 VS 2010 上编译时出现错误 C4430)。这是另一种方法的代码:
标头:
/* VectorSum.h */
#pragma once
#include <vector>
int VectorSum (const vector<int>& values);
源代码:
/* VectorSum.cpp */
#include "VectorSum.h"
#include <vector>
int VectorSum (const vector<int>& values)
{
int S = 0;
for (int t=0; t < values.size(); t++)
{
S += values[t];
}
return S;
}
实现:
/* Main.cpp */
#include "VectorSum.h"
#include <vector>
#include <iostream>
using namespace std;
int main()
{
vector<int> values;
values.push_back(-100);
values.push_back(75);
values.push_back(75);
cout << "Total = " << VectorSum(values) << endl << endl;
cin.ignore(1, '\n');
return 0;
}
如您所见,VectorSum.cpp 中的函数代码与我的第一个 .cpp 文件中的代码相同,因此问题一定出在标头。有什么想法我做错了吗?
I'm having a weird problem compiling a function when I try using multiple files. I've boiled it down to this simple example: suppose I want to find the sum of a vector of integers. If I try compiling the following code, it works as expected:
#include <vector>
#include <iostream>
using namespace std;
int VectorSum (const vector<int>& values)
{
int S = 0;
for (int t=0; t < values.size(); t++)
{
S += values[t];
}
return S;
}
int main()
{
vector<int> values;
values.push_back(-100);
values.push_back(75);
values.push_back(75);
cout << "Total = " << VectorSum(values) << endl << endl;
cin.ignore(1, '\n');
return 0;
}
However, if I try using a header file, it crashes on my (error C4430 when compiling on VS 2010 for Windows XP). Here's the code for the other approach:
the header:
/* VectorSum.h */
#pragma once
#include <vector>
int VectorSum (const vector<int>& values);
the source:
/* VectorSum.cpp */
#include "VectorSum.h"
#include <vector>
int VectorSum (const vector<int>& values)
{
int S = 0;
for (int t=0; t < values.size(); t++)
{
S += values[t];
}
return S;
}
the implementation:
/* Main.cpp */
#include "VectorSum.h"
#include <vector>
#include <iostream>
using namespace std;
int main()
{
vector<int> values;
values.push_back(-100);
values.push_back(75);
values.push_back(75);
cout << "Total = " << VectorSum(values) << endl << endl;
cin.ignore(1, '\n');
return 0;
}
As you can see, the code for the function in VectorSum.cpp is identical to the code in my first .cpp file, so the problem must be in the header. Any ideas what I'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
请参阅C4430 的 MSDN 页面。它是在声明缺少类型或类型未知时发出的。在您的情况下,由于不合格的名称查找规则,
vector
是未知类型。See the MSDN page for C4430. It is issued in case when a declaration is missing a type or the type is unknown. In your case
vector
is an unknown type due to unqualified name lookup rules.这是
std
命名空间的问题。将标头中的声明更改为:
并确保 .cpp 文件中存在
using namespace std;
(如您的第一个示例),或者您使用std
调用/定义函数时适当地使用 code> 命名空间。例如,您需要在VectorSum.cpp
文件中执行以下操作之一。顺便说一句,请不要
在头文件中添加语句。这将强制将命名空间纳入标头的所有用户的范围内(即使它是间接包含的,因此可能不明显),这可能不符合他们的需求。
It's an issue with the
std
namespace.Change the declaration in the header to:
And make sure there's either a
using namespace std;
in the .cpp file(s) (like your first example) or that you use thestd
namespace appropriately when calling/defining the function. For example, you'd need to do one of these things in yourVectorSum.cpp
file.As an aside, please don't add a
statement to the header file. That will force the namespace to be brought into scope for all users of the header (even if it's indirectly included, so it might not be obvious), which might not fit their wants or needs.
问题确实出在头文件中。您忘记添加
到标头,因此编译器不知道
vector
的含义。更正的标题:
The problem is indeed in the header file. You forgot to add
to the header and thus the compiler doesn't know what
vector
means.Corrected header:
您必须在 VectorSum.h 中指定向量的命名空间:
You have to specify the namespace for the vector in VectorSum.h:
添加
usingnamespacestd
尽量避免在头文件中使用usingnamespace,以避免名称冲突。
Add a
using namespace std
Try to avoid using namespace in header files to avoid name conflicts.