我使用 Code::Blocks 做了一些关于 C++ 的学术项目,但我遇到了一些奇怪的可见性错误。
部分主文件是:
//main.cpp
#include <iostream>
#include <fstream>
#define DEFContainerSize 100
#include <string>
#include <list>
#include <sstream>
#include <cctype>
using namespace std;
#include "iteration1.cpp"
#include "iteration2.cpp"
int main(int argNum, char *args[])
{
//Content skipped here!!
}
包含的迭代 1.cpp 包含如下定义:
//部分迭代1.cpp
//Content skipped here!!
template <class Coords, class Rad>
struct
Circle {
Coords Coordinates;
Rad Radius;
};
//Content skipped here!!
template <class Coords, class Rad>
istream
&operator>>(istream &input, Circle<Coords,Rad> &circle)
{
StringFunctionAndArguments fa;
input>>fa;
proc_FromStringFunctionAndArguments2<Coords,Rad>
(fa,"Circle","operator>>",circle.Coordinates,circle.Radius);
return input;
};
template <class Coords, class Rad>
ostream
&operator<<(ostream &output, Circle<Coords,Rad> &circle)
{
output<<"Circle("<<circle.Coordinates<<","<<circle.Radius<<")";
return output;
};
//Content skipped here!!
这很好用。但是当我尝试在iteration2.cpp中放置类似的定义时:
//iteration2.cpp this time full file.
template <class Coords>
struct
Triangle
{
Coords Coordinates1,
Coordinates2,
Coordinates3;
};
template <class Coords>
istream
&operator>>(istream &input, Triangle<Coords> &tr)
{
StringFunctionAndArguments fa;
input>>fa;
proc_FromStringFunctionAndArguments2<Coords,Coords>
(fa,"Triangle","operator>>",tr.Coordinates1,
tr.Coordinates2);
tr.Coordinates3 = fa.Arguments.back();
return input;
};
template <class Coords>
ostream
&operator<<(ostream &output, Triangle<Coords> &tr)
{
output<<"Triangle("<<tr.Coordinates1<<
","<<tr.Coordinates2<<
","<<tr.Coordinates3<<")";
return output;
};
我收到奇怪的错误,这不应该发生:
stud\1mTRPO\labs\1\Lab2\iteration2.cpp|17|error: 'istream' does not name a type|
stud\1mTRPO\labs\1\Lab2\iteration2.cpp|30|error: 'ostream' does not name a type|
||=== Build finished: 2 errors, 0 warnings (0 minutes, 4 seconds) ===|
我仍然可以正常将其添加到迭代 1.cpp,但我需要在迭代 2.cpp 中使用它。
我在 Windows XP 上使用 Code::Blocks 12.11,编译
mingw32-g++.exe -std=c++11 -Wall -fexceptions -g
是时候改变IDE了吗?
我该如何解决这个问题?感谢您提前回答!