C++当我使用标头时,函数会抛出错误,但如果我在源代码中定义它,则不会抛出错误?

发布于 2024-11-27 21:04:24 字数 1578 浏览 4 评论 0原文

当我尝试使用多个文件时,我在编译函数时遇到了一个奇怪的问题。我将其归结为这个简单的示例:假设我想找到整数向量的总和。如果我尝试编译以下代码,它会按预期工作:

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

戏舞 2024-12-04 21:04:25
#pragma once
#include <vector>
int VectorSum (const std::vector<int>& values);
                     ^^^^^

请参阅C4430 的 MSDN 页面。它是在声明缺少类型或类型未知时发出的。在您的情况下,由于不合格的名称查找规则,vector 是未知类型。

#pragma once
#include <vector>
int VectorSum (const std::vector<int>& values);
                     ^^^^^

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.

烧了回忆取暖 2024-12-04 21:04:25

这是 std 命名空间的问题。

将标头中的声明更改为:

int VectorSum (const std::vector<int>& values);

并确保 .cpp 文件中存在 using namespace std; (如您的第一个示例),或者您使用 std调用/定义函数时适当地使用 code> 命名空间。例如,您需要在 VectorSum.cpp 文件中执行以下操作之一。


顺便说一句,请不要

using namespace std;

在头文件中添加语句。这将强制将命名空间纳入标头的所有用户的范围内(即使它是间接包含的,因此可能不明显),这可能不符合他们的需求。

It's an issue with the std namespace.

Change the declaration in the header to:

int VectorSum (const std::vector<int>& values);

And make sure there's either a using namespace std; in the .cpp file(s) (like your first example) or that you use the std namespace appropriately when calling/defining the function. For example, you'd need to do one of these things in your VectorSum.cpp file.


As an aside, please don't add a

using namespace std;

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.

丘比特射中我 2024-12-04 21:04:25

问题确实出在头文件中。您忘记添加

using namespace std;

到标头,因此编译器不知道 vector 的含义。

更正的标题:

/* VectorSum.h */
#pragma once
#include <vector>
using namespace std;  // This was missing
int VectorSum (const vector<int>& values); // Now OK, the compiler knows vector

The problem is indeed in the header file. You forgot to add

using namespace std;

to the header and thus the compiler doesn't know what vector means.

Corrected header:

/* VectorSum.h */
#pragma once
#include <vector>
using namespace std;  // This was missing
int VectorSum (const vector<int>& values); // Now OK, the compiler knows vector
梦境 2024-12-04 21:04:25

您必须在 VectorSum.h 中指定向量的命名空间:

int VectorSum (const std::vector<int>& values);

You have to specify the namespace for the vector in VectorSum.h:

int VectorSum (const std::vector<int>& values);
逆光下的微笑 2024-12-04 21:04:25

添加usingnamespacestd

/* VectorSum.h */
#pragma once
#include <vector>
<--------
//using namespace std;
int VectorSum (const std::vector<int>& values);

尽量避免在头文件中使用usingnamespace,以避免名称冲突。

Add a using namespace std

/* VectorSum.h */
#pragma once
#include <vector>
<--------
//using namespace std;
int VectorSum (const std::vector<int>& values);

Try to avoid using namespace in header files to avoid name conflicts.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文