字段 'class' 有一个不兼容的 'class'
field 'class' has an incompatible 'class'
我收到这些错误:
node.h:12:2: error: 'Student' does not name a typeStudent student;
node.h:17:14: error: 'Student' does not name a typeNode( const Student& );
node.h:20:25: error: 'Student' does not name a type
void setStudent( const Student& );
node.h:21:2: error: 'Student' does not name a type
Student getStudent() const { return student; }
这是我从 student.h
中删除包含 node.h 时发生的情况
node.cpp:13: first defined here
node.cpp.o: In function `Node::~Node()'
node.cpp:13: multiple definition of `Node::Node()'
node.cpp:13: first defined here
node.cpp.o: In function `Node::~Node()':
node.cpp:17: multiple definition of `Node::Node(Node const&)'
main.cpp.o:node.cpp:17: first defined here
node.cpp.o: In function `Node::~Node()':
node.cpp:17: multiple definition of `Node::Node(Node const&)'
main.cpp.o:C:node.cpp:17: first defined here
node.cpp.o: In function `Node::Node(Student const&)':
node.cpp:25: multiple definition of `Node::Node(Student const&)'
node.cpp:25: first defined here
在node.h
#pragma once
#ifndef node_h
#define node_h
#include "student.h"
class Node
{
private:
Student student;
Node* nextPtr;
Node* prevPtr;
public:
Node();
Node( const Student& );
Node( const Node& );
virtual ~Node() {}
void setStudent( const Student& );
Student getStudent() const { return student; } //second error here
//linked list needs to be set as a friend
friend class dblinkedlist;
};
#endif
在 student.h 我不明白为什么这不能正常工作我已经尝试多次更改代码,但无济于事。
#ifndef STUDENT_H
#define STUDENT_H
#include <stdio.h>
#include <string>
#include "node.h"
using namespace std;
class Student
{
private:
string FirstName;
string LastName;
int idNumber;
double Gpa;
public:
//constructors
Student();
Student( string, string, int, double );
//copy construtor
Student( const Student& );
//destructor
virtual ~Student(){}
//set/get for private data
void setidNumber( int i );
int getidNumber() const { return idNumber; }
void setFirstName( string );
string getFirstName( string ) const { return FirstName; }
void setLastName( string );
string getLastName( string ) const { return LastName; }
void setGpa( double g );
double getGpa( double ) const { return Gpa; }
void setStudent( const Student& );
Student getStudent() const { return *this; }
//overloaded assignment
Student operator=( const Student& );
//overloaded << and >>
friend ostream& operator<<( ostream&, const Student& );
friend istream& operator>>( istream&, Student& );
//overloaded ==
friend bool operator==( const Student&, const Student& );
//make Node class a friend
friend class Node;
};
#endif
我尝试了一些其他方法来修复错误,但没有得出结论,非常感谢您的帮助。
我对这两行深感困惑:
#include "student.h"
class Student;
我不认为第二个是必要的。
这是在说 "hey compiler, go get the definition of the class Student from this here header file so that I can use it in this class and you'll know about its members and methods and we will both be happy",紧接着是 "hey compiler, forget about that complete class definition I just gave you, instead, I'm declaring that a class Student exists, but I'm only going to tell you more about it later"。编译器被正确地混淆了。
对于你的第二个(第一个?)错误,你有两个 headers 包括彼此。而且我不明白为什么,因为 Student 中对 Node 的唯一引用是将其添加为好友,而 Node 从 Student 中唯一使用的是 get 和 setStudent(),它们是 public 方法。我会尝试删除 friend class Node
和 #include "node.h"
以查看是否有所不同。
问题是 student.h 包含 node.h,它需要 Student 的完整定义,因此它包含 student.h,但由于我们已经在处理 student.h header guards 已经被定义但什么都没有,所以它在 node.h 中继续并到达一个 Student 变量,但它还没有被定义,因为即使我们从 student.h 开始,我们实际上从未找到学生 class.
解决方案是从 student.h
中删除 node.h 的包含
我收到这些错误:
node.h:12:2: error: 'Student' does not name a typeStudent student;
node.h:17:14: error: 'Student' does not name a typeNode( const Student& );
node.h:20:25: error: 'Student' does not name a type
void setStudent( const Student& );
node.h:21:2: error: 'Student' does not name a type
Student getStudent() const { return student; }
这是我从 student.h
中删除包含 node.h 时发生的情况node.cpp:13: first defined here
node.cpp.o: In function `Node::~Node()'
node.cpp:13: multiple definition of `Node::Node()'
node.cpp:13: first defined here
node.cpp.o: In function `Node::~Node()':
node.cpp:17: multiple definition of `Node::Node(Node const&)'
main.cpp.o:node.cpp:17: first defined here
node.cpp.o: In function `Node::~Node()':
node.cpp:17: multiple definition of `Node::Node(Node const&)'
main.cpp.o:C:node.cpp:17: first defined here
node.cpp.o: In function `Node::Node(Student const&)':
node.cpp:25: multiple definition of `Node::Node(Student const&)'
node.cpp:25: first defined here
在node.h
#pragma once
#ifndef node_h
#define node_h
#include "student.h"
class Node
{
private:
Student student;
Node* nextPtr;
Node* prevPtr;
public:
Node();
Node( const Student& );
Node( const Node& );
virtual ~Node() {}
void setStudent( const Student& );
Student getStudent() const { return student; } //second error here
//linked list needs to be set as a friend
friend class dblinkedlist;
};
#endif
在 student.h 我不明白为什么这不能正常工作我已经尝试多次更改代码,但无济于事。
#ifndef STUDENT_H
#define STUDENT_H
#include <stdio.h>
#include <string>
#include "node.h"
using namespace std;
class Student
{
private:
string FirstName;
string LastName;
int idNumber;
double Gpa;
public:
//constructors
Student();
Student( string, string, int, double );
//copy construtor
Student( const Student& );
//destructor
virtual ~Student(){}
//set/get for private data
void setidNumber( int i );
int getidNumber() const { return idNumber; }
void setFirstName( string );
string getFirstName( string ) const { return FirstName; }
void setLastName( string );
string getLastName( string ) const { return LastName; }
void setGpa( double g );
double getGpa( double ) const { return Gpa; }
void setStudent( const Student& );
Student getStudent() const { return *this; }
//overloaded assignment
Student operator=( const Student& );
//overloaded << and >>
friend ostream& operator<<( ostream&, const Student& );
friend istream& operator>>( istream&, Student& );
//overloaded ==
friend bool operator==( const Student&, const Student& );
//make Node class a friend
friend class Node;
};
#endif
我尝试了一些其他方法来修复错误,但没有得出结论,非常感谢您的帮助。
我对这两行深感困惑:
#include "student.h"
class Student;
我不认为第二个是必要的。
这是在说 "hey compiler, go get the definition of the class Student from this here header file so that I can use it in this class and you'll know about its members and methods and we will both be happy",紧接着是 "hey compiler, forget about that complete class definition I just gave you, instead, I'm declaring that a class Student exists, but I'm only going to tell you more about it later"。编译器被正确地混淆了。
对于你的第二个(第一个?)错误,你有两个 headers 包括彼此。而且我不明白为什么,因为 Student 中对 Node 的唯一引用是将其添加为好友,而 Node 从 Student 中唯一使用的是 get 和 setStudent(),它们是 public 方法。我会尝试删除 friend class Node
和 #include "node.h"
以查看是否有所不同。
问题是 student.h 包含 node.h,它需要 Student 的完整定义,因此它包含 student.h,但由于我们已经在处理 student.h header guards 已经被定义但什么都没有,所以它在 node.h 中继续并到达一个 Student 变量,但它还没有被定义,因为即使我们从 student.h 开始,我们实际上从未找到学生 class.
解决方案是从 student.h
中删除 node.h 的包含