gpt4 book ai didi

c++ - 使用未定义类型

转载 作者:太空宇宙 更新时间:2023-11-04 15:54:10 24 4
gpt4 key购买 nike

//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;
}

错误消息:'错误 1 ​​error C2027: 使用未定义类型 'st::My_List' 13'

为什么?特别是如果我评论这一行,它将被编译并且 create() 正在使用这种类型。

最佳答案

您只在头文件list.h 中做了前向声明。实际定义在 list.cpp 中。在编译 main.cpp 时,您包含了 list.h,因此编译器不知道 My_List 类中有哪些方法。需要将struct定义移动到头文件list.h中,方法实现仍然保留在list.cpp中

关于c++ - 使用未定义类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2606897/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com