C++ 使用未定义的类型 //文件列表.h #包括“stdafx.h” 名称空间st { 构造我的列表; 键入定义我的清单; 列表*创建(常量字符*名称); } //文件列表.cpp #包括“stdafx.h” #包括“list.h” 名称空间st { 构造我的列表 { 常量字符*名称; 我的清单*左; 我的清单*对; 我的列表(常量字符*名称):名称(名称), 左(nullptr),, 右(nullptr) {} 我的列表(const My_List&); ~My_List() { } 无效插入(我的清单*); 无效集合名称(字符*名称) { 姓名=姓名; } 常量字符*get_name()常量 { 返回名称; } }; 键入定义我的清单; /*optor+的助手类*/ 结构MyChar { 常量字符*我的数据; MyChar(const char*c_string):我的数据(c_string){ 运算符常量字符*() { 返回我的\u数据\u; } 运算符char*() { 返回常量转换(我的数据); } }; 字符*运算符+(MyChar左\右\ MyChar) { 如果(!左\我的\数据\右\我的\数据\右) { 返回0; } size\u t size=1;//数组中最后一个'\0'字符的大小设置为1 char*p=”“;//如果两个c_字符串都为空,则返回 布尔有左=假; 布尔有权=假; if(strlen(左)) { 大小+=strlen(左); has_left_uu=true; } 如果(斯特伦(右) { 大小+=strlen(右); has_right_u=true; } bool all=has_left_&&has_right_?true:false; 如果(两者) { p=新字符[大小](); const void*p_v=p;//只保留p开头的地址 const char*tmp=左; /*复制第一个c_字符串*/ 而(*p++=*tmp++); tmp=右u2;; /*离最后一圈太远了*/ --p; 而(*p++=*tmp++); *p='\0'; /*返回到数组的开头*/ p=静态施法(常数施法(p_v)); 返回p; } 否则如果(你离开了) { 向左返回; } 否则如果(有权) { 返回权; } return p;//如果两个c_字符串都为空,则返回“” } My_List::My_List(const My_List&pat):左(nullptr),右(nullptr) { name=pat.name+MyChar(“\u cpy”); 我的列表*pattern=const\u cast(&pat); 我的清单*目标=这个; while(模式->右侧) { 目标->右=静态施法(malloc(sizeof(My_List)); *目标->右\=*模式->右\; 目标->右->设置名称(模式->右->获取名称()+MyChar(“\u cpy”); 目标->右->左=静态施法(malloc(sizeof(myu列表)); *目标->右->左->模式->右->左; 目标->右->左->设置名称(模式->右->左->获取名称(); 图案=图案->右侧; 目标=目标->右; } } 作废我的清单::插入(我的清单*obj) { /*抓第一根树枝*/ 我的清单*tmp=这个; 如果(tmp->右) { /*走到右边的尽头*/ 同时(tmp->右) { tmp=tmp->右; } tmp->右=obj; obj->左=tmp; } 其他的 { tmp->右=obj; obj->左=此; } } 我的列表*创建(常量字符*名称) { 返回新的My_列表(名称); } } //文件main.cpp #包括“stdafx.h” #包括“list.h” 使用名称空间st; int _tmain(int argc,_TCHAR*argv[] { 列表*我的=创建(“a”); 列表*b=创建(“b”); my->insert(b);//这里有一个错误 返回0; }
错误消息: '错误1错误C2027:使用未定义类型'st::My_列表'13'C++ 使用未定义的类型 //文件列表.h #包括“stdafx.h” 名称空间st { 构造我的列表; 键入定义我的清单; 列表*创建(常量字符*名称); } //文件列表.cpp #包括“stdafx.h” #包括“list.h” 名称空间st { 构造我的列表 { 常量字符*名称; 我的清单*左; 我的清单*对; 我的列表(常量字符*名称):名称(名称), 左(nullptr),, 右(nullptr) {} 我的列表(const My_List&); ~My_List() { } 无效插入(我的清单*); 无效集合名称(字符*名称) { 姓名=姓名; } 常量字符*get_name()常量 { 返回名称; } }; 键入定义我的清单; /*optor+的助手类*/ 结构MyChar { 常量字符*我的数据; MyChar(const char*c_string):我的数据(c_string){ 运算符常量字符*() { 返回我的\u数据\u; } 运算符char*() { 返回常量转换(我的数据); } }; 字符*运算符+(MyChar左\右\ MyChar) { 如果(!左\我的\数据\右\我的\数据\右) { 返回0; } size\u t size=1;//数组中最后一个'\0'字符的大小设置为1 char*p=”“;//如果两个c_字符串都为空,则返回 布尔有左=假; 布尔有权=假; if(strlen(左)) { 大小+=strlen(左); has_left_uu=true; } 如果(斯特伦(右) { 大小+=strlen(右); has_right_u=true; } bool all=has_left_&&has_right_?true:false; 如果(两者) { p=新字符[大小](); const void*p_v=p;//只保留p开头的地址 const char*tmp=左; /*复制第一个c_字符串*/ 而(*p++=*tmp++); tmp=右u2;; /*离最后一圈太远了*/ --p; 而(*p++=*tmp++); *p='\0'; /*返回到数组的开头*/ p=静态施法(常数施法(p_v)); 返回p; } 否则如果(你离开了) { 向左返回; } 否则如果(有权) { 返回权; } return p;//如果两个c_字符串都为空,则返回“” } My_List::My_List(const My_List&pat):左(nullptr),右(nullptr) { name=pat.name+MyChar(“\u cpy”); 我的列表*pattern=const\u cast(&pat); 我的清单*目标=这个; while(模式->右侧) { 目标->右=静态施法(malloc(sizeof(My_List)); *目标->右\=*模式->右\; 目标->右->设置名称(模式->右->获取名称()+MyChar(“\u cpy”); 目标->右->左=静态施法(malloc(sizeof(myu列表)); *目标->右->左->模式->右->左; 目标->右->左->设置名称(模式->右->左->获取名称(); 图案=图案->右侧; 目标=目标->右; } } 作废我的清单::插入(我的清单*obj) { /*抓第一根树枝*/ 我的清单*tmp=这个; 如果(tmp->右) { /*走到右边的尽头*/ 同时(tmp->右) { tmp=tmp->右; } tmp->右=obj; obj->左=tmp; } 其他的 { tmp->右=obj; obj->左=此; } } 我的列表*创建(常量字符*名称) { 返回新的My_列表(名称); } } //文件main.cpp #包括“stdafx.h” #包括“list.h” 使用名称空间st; int _tmain(int argc,_TCHAR*argv[] { 列表*我的=创建(“a”); 列表*b=创建(“b”); my->insert(b);//这里有一个错误 返回0; },c++,C++,错误消息: '错误1错误C2027:使用未定义类型'st::My_列表'13' 为什么??特别是,如果我注释这一行,它将被编译,create()使用的是这种类型。您只在头文件list.h中做了一个转发声明。实际定义在list.cpp中。编译main.cpp时包含list.h,所以编译器不知道类My_list中有哪些方法。您需要将结构定义移动到头文件list.h,您仍然可以在list.cpp中保留方法实现。您只在头文件list.h中做了一个转发声明。实际定义在list.cpp中。编译main.cp
为什么??特别是,如果我注释这一行,它将被编译,create()使用的是这种类型。您只在头文件list.h中做了一个转发声明。实际定义在list.cpp中。编译main.cpp时包含list.h,所以编译器不知道类My_list中有哪些方法。您需要将结构定义移动到头文件list.h,您仍然可以在list.cpp中保留方法实现。您只在头文件list.h中做了一个转发声明。实际定义在list.cpp中。编译main.cpp时包含list.h,所以编译器不知道类My_list中有哪些方法。您需要将结构定义移动到头文件list.h中,您仍然可以将方法实现保留在list.cpp糟糕的问题名称中,我的意思是,除了我之外,为什么会有人费心阅读此文件,问题是什么??更好地解释->更好地回答使用类而不是结构,以可读的方式声明类型/类而不是此处或此处的typedef可怕的问题名称,我的意思是,除了我之外,为什么有人会费心阅读这个,问题是什么??Explain better->get better answersTry使用类而不是结构,以可读的方式声明类型/类而不是typedef在这里或那里,然后也许你可以“接受”答案:)我当时没有接受答案,因为我不得不离开办公室,由于接受标记上的时间限制,无法立即接受。为什么有一个时间限制是超越我。如果没有时间限制,我会接受他的回答,因为我打算这么做,但由于这个荒谬的时间限制,我现在必须解释自己,而不是仅仅接受这个按钮。当然,给Meta写信是没有意义的——他们总是有非常合乎逻辑的解释为什么会这样做。然后也许你可以“接受”答案:)我没有接受answ
//file list.h
#include "stdafx.h"
namespace st
{
struct My_List;
typedef My_List list;
list* create(const char* name);
}
//file list.cpp
#include "stdafx.h"
#include "list.h"
namespace st
{
struct My_List
{
const char* name_;
My_List* left_;
My_List* right_;
My_List(const char* name):name_(name),
left_(nullptr),
right_(nullptr)
{}
My_List(const My_List&);
~My_List()
{
}
void insert(My_List*);
void set_name(char* name)
{
name_ = name;
}
const char* get_name()const
{
return name_;
}
};
typedef My_List list;
/*helper class for optor+ */
struct MyChar
{
const char* my_data_;
MyChar(const char* c_string):my_data_(c_string){}
operator const char*()
{
return my_data_;
}
operator char*()
{
return const_cast<char*>(my_data_);
}
};
char* operator+(MyChar left_, MyChar right_)
{
if (!left_.my_data_ || !right_.my_data_)
{
return 0;
}
size_t size = 1;//size is set to one for final '\0' char in an array
char* p = "";//if both c_strings are empty this is returned
bool has_left_ = false;
bool has_right_ = false;
if (strlen(left_))
{
size += strlen(left_);
has_left_ = true;
}
if (strlen(right_))
{
size += strlen(right_);
has_right_ = true;
}
bool both = has_left_ && has_right_ ? true : false;
if (both)
{
p = new char[size]();
const void* p_v = p;//just to keep address of beginning of p
const char* tmp = left_;
/*copying first c_string*/
while (*p++ = *tmp++);
tmp = right_;
/*one too far after last loop*/
--p;
while (*p++ = *tmp++);
*p = '\0';
/*go back to the beginning of an array*/
p = static_cast<char*>(const_cast<void*>(p_v));
return p;
}
else if (has_left_)
{
return left_;
}
else if (has_right_)
{
return right_;
}
return p;//returns "" if both c_strings were empty
}
My_List::My_List(const My_List& pat):left_(nullptr),right_(nullptr)
{
name_ = pat.name_ + MyChar("_cpy");
My_List* pattern = const_cast<My_List*>(&pat);
My_List* target = this;
while (pattern->right_)
{
target->right_ = static_cast<My_List*>(malloc(sizeof(My_List)));
*target->right_ = *pattern->right_;
target->right_->set_name(pattern->right_->get_name() + MyChar("_cpy"));
target->right_->left_ = static_cast<My_List*>(malloc(sizeof(My_List)));
*target->right_->left_ = *pattern->right_->left_;
target->right_->left_->set_name(pattern->right_->left_->get_name() + MyChar("_cpy"));
pattern = pattern->right_;
target = target->right_;
}
}
void My_List::insert(My_List* obj)
{
/*to catch first branch*/
My_List* tmp = this;
if (tmp->right_)
{
/*go to the end of right side*/
while (tmp->right_)
{
tmp = tmp->right_;
}
tmp->right_ = obj;
obj->left_ = tmp;
}
else
{
tmp->right_ = obj;
obj->left_= this;
}
}
My_List* create(const char* name)
{
return new My_List(name);
}
}
//file main.cpp
#include "stdafx.h"
#include "list.h"
using namespace st;
int _tmain(int argc, _TCHAR* argv[])
{
list* my = create("a");
list* b = create("b");
my->insert(b);//HERE I'M GETTING ERROR
return 0;
}