【发布时间】:2013-04-28 13:07:43
【问题描述】:
以下代码在 Visual Studio 2012 Express、Windows 8 下编译得很好
但在我首选的平台上,Eclipse Juno,OS X 上的 GCC 4.2 我收到以下错误:
../src/Test.cpp:20: error: 'std::istream& TestNS::operator>>(std::istream&, TestNS::Test&)' 应该在'TestNS'中声明
#include <cstdio>
#include <cstdlib>
#include <iostream>
using std::istream;
namespace TestNS
{
class Test
{
friend istream &operator>>(istream &in, Test &value);
public:
Test(double real, double image);
private:
double real;
double image;
void initialize(double real, double image);
};
}
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include "Header.h"
using std::istream;
using namespace TestNS;
TestNS::Test::Test(double real = 0.0, double image = 0.0) : real(real), image(image)
{
}
void TestNS::Test::initialize(double real, double image)
{
this->real = real;
this->image = image;
}
istream& TestNS::operator>> (istream &in, TestNS::Test &value)
{
value.real = 10.0;
value.image = 10.0;
return in;
}
int main()
{
}
任何帮助都会非常有帮助。 为学校项目工作。
【问题讨论】:
-
TestNS 不是类,是头文件中的命名空间,教授表示需要。
-
赋值表示必须是好友函数。
-
没有TestNS::函数是全局函数不是友元函数,代码在Windows、Visual Studio下编译,但不是Eclipse GCC OS X
-
它在 OS X 下使用 GCC 4.2 编译
-
我重新构建了一个全新的项目,仍然不行,路径和符号,包含路径设置为:/usr/include/c++/4.2.1。