名称空间错误
Error with namespaces
嗨,我正在做一个作业,在课堂上熟悉使用名称空间。我得到了一些不同的错误,虽然我不知道为什么,因为我已经在其他4个头和。cpp文件w/o错误做了同样的过程,所以我被难住了。错误如下:
WriteFile.cpp: In function 'void WriteLine(CSC2100::WriteFile*, CSC2100::String*)':
WriteFile.cpp:39:27: error: invalid use of incomplete type 'struct CSC2100::String'
In file included from WriteFile.h:4:0,
from WriteFile.cpp:1;
String.h:6:11: error: forward declaration of 'struct CSC2100::String'
WriteFile.cpp:41:30: error: invalid use of incomplete type 'struct CSC2100::String'
In file included from WriteFile.h:4:0,
from Writefile.cpp:1:
String.h:6:11: error: forward declaration of 'struct CSC 2100::String'
我已经彻底检查了错误中提到的代码,但似乎找不到问题。我在WriteFile.cpp的顶部包含了前向声明以及错误所在的函数:
#include "WriteFile.h"
namespace CSC2100
{
struct WriteFile
{
ofstream output_file;
bool closed;
};
}
#include <sstream>
using namespace CSC2100;
///////////////////////////////////////////
void writeLine(WriteFile* wf, String* line)
{
if (!wf->closed && line->sz > 0)
{
wf->output_file << line->text << endl;
}
}
这里是WriteFile.h:
#include "String.h"
#include <fstream>
using namespace std;
namespace CSC2100
{
struct WriteFile;
}
CSC2100::WriteFile* createWriteFile(const char* file_name);
void destroyWriteFile(CSC2100::WriteFile* wf);
void writeLine(CSC2100::WriteFile* wf, CSC2100::String* line);
void close(CSC2100::WriteFile* wf);
我将继续包括错误中提到的String.h。String.cpp:
中结构体的前向声明namespace CSC2100
{
struct String
{
const char* text;
int sz;
};
}
和String.h:
#if !defined STRING_STRUCT
#define STRING_STRUCT
namespace CSC2100
{
struct String;
}
CSC2100::String* createString(const char* char_array);
void displayString(CSC2100::String* str);
void destroyString(CSC2100::String* str);
int length(CSC2100::String* str);
const char* getText(CSC2100::String* str);
int a_to_i(CSC2100::String* str);
float a_to_f(CSC2100::String* str);
CSC2100::String* i_to_a(int number);
CSC2100::String* f_to_a(float number);
int find(CSC2100::String* str, char delimiter, int start);
CSC2100::String* substr(CSC2100::String* str, int start, int end);
int compare(CSC2100::String* str1, CSC2100::String* str2);
代码应该和教授写的一样正确。我们所做的就是给它应用一个名称空间。
此代码
namespace CSC2100 {
struct String {
const char* text;
int sz;
};
}
当然应该转到String.h
而不是String.cpp
!!
<参见:包括. cpp文件 子>
对于WriteFile.h
,尝试(不包括String.h
):
#include <fstream>
// #include "String.h" **** OMIT THIS ****
namespace CSC2100 {
class String;
}
// ...
并在WriteFile.cpp
中包含String.h
!
必须使用前向声明类声明(无论它们是否在特定的命名空间中)来解析这种循环引用。
相关文章:
- 使用命名空间时出现多个定义错误
- 使用 make 编译 MPI,几个命名空间错误,例如"错误:未知类型名称'使用'?
- 从类型列表中递归删除重复项会导致编译器堆空间错误 (VS2017)
- 我该怎么做才能摆脱使用命名空间时的语法错误?
- C++ Visual Studio 错误:名称后跟'::'必须是类或命名空间名称 (DirectX 11)
- 在 C++ 中使用命名空间时出现"does not name a type"错误
- C++命名空间中调用 void 函数的错误
- 无法获取 wig %ignore 以忽略命名空间中的类,从而导致编译错误
- 为什么 Clang 在命名空间'std'中没有名为 'copy' 的成员给出错误
- 名称后跟"::"必须是类或命名空间错误,即使"::"后面是类名
- Catalina C++:使用<cmath>标头产生错误:全局命名空间中没有名为 'signbit' 的成员
- Visual C:命名空间范围错误或晦涩功能?
- 共享库中使用"使用命名空间"定义的符号错误
- 如何解决 comiler 错误:'v_bias'不是命名空间名称
- fstream.h 中的 STL 命名空间错误与 qmake/make.
- C++ ,错误:命名空间'std'中的'shared_ptr'未命名模板类型
- 错误:命名空间"std"中没有名为"invoke_result_t"的类型 GCC-7
- 为什么在匿名名称空间中定义的模板时,为什么内部链接错误
- boost :: filesystem :: space()报告错误的磁盘空间
- 使用getline()后出现错误空间