编译错误:2 个重载没有'this'指针的合法转换。使用结构
Compile Error: 2 overloads have no legal conversion for 'this' pointer. Working with structs
我的编程作业要我定义一个具有名称向量的结构 Student 和一个具有名称和包含已注册学生和以下函数的向量的结构 Program:
void print_student(Student* s)
void print_course(Course* c)
void enroll(Student* s, Course* c)
//enrolls given student in the given course and updates both vectors
我尝试在注册函数参数中添加与号来修复它,但它不起作用。
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Student
{
string Name ;
vector < Course* > Courses;
};
struct Course
{
string Name ;
vector < Student* > Students;
};
void print_Student(Student* s)
{
cout << s->Name << endl;
for (int i = 0; i < s->Courses.size(); i++)
{
cout << s->Courses[i] << endl;
}
};
void print_course(Course* c)
{
cout << c->Name << endl;
for (int i = 0; i < c->Students.size(); i++)
{
cout << c->Students[i] << endl;
}
};
void enroll(Student* &s, Course* &c)
{
cout << "Enrolled " << s << "in " << c << endl;
s->Courses.push_back( c );
c->Students.push_back( s);
}
int main()
{
Student* Bob;
Course* ComputerScience;
Bob->Name = "Bob";
ComputerScience->Name = "Computer Science";
enroll( Bob , ComputerScience);
system("Pause");
}
我希望代码能够让学生 Bob 注册到计算机科学课程,以便我以后可以定义更多学生并打印他们。
代码看起来不错,但是在运行编译器时给我以下错误:
source.cpp(10): error C2065: 'Course': undeclared identifier
source.cpp(10): error C2059: syntax error: '>'
source.cpp(10): error C2976: 'std::vector': too few template arguments
source.cpp(41): error C2663: 'std::vector<_Ty,_Alloc>::push_back': 2 overloads have no legal conversion for 'this' pointer
我对发生了什么感到困惑,我该如何解决它?
编译代码时,编译器在编译结构 Course 之前开始编译结构 Student 。因此,在编译学生时,编译器不知道课程是什么。因此错误,未声明的标识符。要修复它,请像这样向前声明结构 Course:
struct Course;
struct Student
{
string Name ;
vector < Course* > Courses;
};
struct Course
{
string Name ;
vector < Student* > Students;
};
相关文章:
- JSON转换为nlohmann JSON-lib中的结构数组
- NLOHMANN 的 JSON 库将数组转换为结构向量
- 从成员指针到整个结构/类的强制转换
- 在nlohmann json中,如何将嵌套对象的数组转换为嵌套结构的向量
- 将字符数组转换为时间结构
- 使用带有链表的堆栈数据结构将中缀转换为后缀
- i2d_X509_REQ_INFO无法正确转换req_info结构
- 在C++中将结构转换和存储为二进制
- 将字符数组转换为结构时出现问题. 结构的字符数组变量溢出
- 转换包含向量 C++ 的语法和结构
- 正在转换结构数据的字节序
- 如何大致将 cpp 代码结构转换为 python
- 将结构 std::memcpy 转换为具有足够容量的 std::vector 是未定义的行为<char>吗?
- C++ 向下转换结构
- 尝试在C++中转换结构时出现奇怪的错误消息
- 向向下转换结构添加属性值会导致分段错误
- C++基于子类型动态强制转换结构
- 从字节缓冲区强制转换结构
- c++中的类型转换结构
- c++的类型转换结构指针指向类型值