gpt4 book ai didi

c++ - 字段 'class' 有一个不兼容的 'class'

转载 作者:行者123 更新时间:2023-11-27 22:59:28 26 4
gpt4 key购买 nike

我收到这些错误:

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 中删除 include 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

我尝试了一些其他方法来修复错误,但没有得出结论,非常感谢任何帮助。

最佳答案

问题是 student.h 包含了 node.h,它需要 Student 的完整定义,所以它包含了 student.h,但是由于我们已经在处理 student.h,header guards 已经定义了,什么也没有,所以它在 node.h 中继续并到达 Student 变量,但它尚未定义,因为即使我们从 student.h 开始,我们也从未真正到达 Student 类。

解决方案是从 student.h 中删除 node.h 的包含

关于c++ - 字段 'class' 有一个不兼容的 'class',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29224047/

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