使用g ++获得一个非常奇怪的编译错误 [英] Getting a really weird compilation error with g++

查看:73
本文介绍了使用g ++获得一个非常奇怪的编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个文件:main.cpp,Euler.h和Euler.cpp。在Euler.cpp中,我有一个使用Euler筛子计算素数的函数:

I have three files: main.cpp, Euler.h, and Euler.cpp. In Euler.cpp, I have a function that calculates prime numbers using Euler''s sieve:

< span class =codeLinkonclick =Blur(this,this.parentNode.parentNode,getChildren(this),true);>展开 | 选择 | Wrap | 行号

推荐答案


#ifndef EULER_H

#define EULER_H


#include< vector> ;

#include" Euler.cpp"


vector< int>筛(vector< int>& primelist,int i);


#endif
#ifndef EULER_H
#define EULER_H

#include <vector>
#include "Euler.cpp"

vector<int> sieve(vector<int>& primelist, int i);

#endif



您不要将代码放在头文件中。也就是说,头文件用于声明和其他不创建变量或生成机器指令的东西。


因此看起来你不需要Euler.h文件。在main.cpp中,您应该包含< vector>。然后在main.cpp中创建向量变量。


Euler.cpp应编译为构建的第二个文件。如果它需要在main.cpp中创建的向量,那么你可以在Euler.cpp中为该变量声明一个extern。

You do not put code in header files. That is, a header file is for declarations and other things that do not create variables or otherwise generate machine instructions.

So it appears you do not need the Euler.h file. In main.cpp you should include <vector>. Then create the vector variable in main.cpp.

Euler.cpp should compile as a second file of your build. If it needs the vector created in main.cpp, then you can declare an extern in Euler.cpp for that variable.


感谢你的回复,但我仍然得到了同样的结果。错误。代码改为:


/ **删除EULER.H ** /


main.cpp:

删除了Euler.h include,改为:
Thanks for the reply, but I''m still getting the same error. Code changed to this:

/**REMOVED EULER.H**/

main.cpp:
Removed the Euler.h include, and instead put:
展开 | 选择 | Wrap | 行号


只是外部人员不会这样做。您需要在euler.cpp中对该函数进行extern:

Just an extern won''t do it. You need to extern to the function in euler.cpp:

展开 | 选择 | Wrap | 行号


这篇关于使用g ++获得一个非常奇怪的编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆