【问题标题】:field 'class' has an incompatible 'class'字段“类”具有不兼容的“类”
【发布时间】:2015-03-24 02:32:09
【问题描述】:

我收到以下错误:

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

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

【问题讨论】:

  • 如果前向声明一个类,它只能用作指针或引用。 Student student 不是这些,因此需要Student 的完整定义。我假设它在 student.h 中,但您没有包含此文件。
  • 从 Student.h 中删除 #include "node.h",这应该可以解决问题。如果需要,您可以将其放入 Student.cpp(包括 Student.h 之后)。
  • @MattMcNabb 我完全按照你所说的做了,但是我收到了上面发布的错误
  • 要么你在你的 makefile / 项目文件中搞砸了一些东西,要么你的一个文件正在做#include "node.cpp"

标签: c++ function class compiler-errors incompatibility


【解决方案1】:

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

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

【讨论】:

  • 我确实这样做了,但我遇到了很多错误。发布在上面。
【解决方案2】:

对于您的第二个(第一个?)错误,您的两个标题都包含彼此。而且我不明白为什么,因为 Student 中对 Node 的唯一引用是与它成为朋友,而 Node 从 Student 中使用的唯一东西是 get & setStudent(),它们是公共方法。我会尝试删除 friend class Node#include "node.h" 看看是否有区别。

【讨论】:

  • 我尝试将两者都删除,但它只会给我“多重定义”错误
  • @CodeHero:发布更新的代码和新的错误。我们会得到这个。
  • @CodeHero:另外,我注意到您在底部的 node.h 中缺少#endif
  • @CodeHero:我注意到你的更新,还有class Student;是你的代码吗?
  • 不,它不在我的代码中,我忘了在那里删除它。
【解决方案3】:

这两行让我很困惑:

#include "student.h"
class Student;

我不认为第二个是必要的。

这是在说“嘿编译器,从这里的头文件中获取 Student 类的定义,这样我就可以在这个类中使用它,你就会知道它的成员和方法,我们都会快乐”紧随其后的是“嘿编译器,忘记我刚刚给你的完整类定义,相反,我声明一个学生类存在,但我只会在稍后告诉你更多关于它的信息”。编译器被正确混淆了。

【讨论】:

  • 它给了我一个错误,指出学生没有命名类型,然后如果我这样做
  • @CodeHero:请包含 Student.h 文件和其他错误消息(在您的主要问题中)。
  • @RonThompson 我添加了我的 student.h 文件,如果我删除学生类,我会得到错误;行
猜你喜欢
  • 2015-08-18
  • 2020-04-05
  • 1970-01-01
  • 2014-02-23
  • 1970-01-01
  • 2013-09-17
  • 1970-01-01
  • 2012-01-04
相关资源
最近更新 更多