Search code examples
c++functionclasscompiler-errorsincompatibility

field 'class' has an incompatible 'class'


I get these errors:

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

this is what happens when I remove the include node.h from student.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

in 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

in student.h I do not see why this is not working properly I have tried changing the code many times, but to no avail.

  #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

I have tried some other methods in trying to fix the error but have come to no conclusion, any help is greatly appreciated.


Solution

  • For your second (first?) error, you've got both headers including each other. And I don't understand why, since the only reference to Node in Student is to friend it, and the only thing Node uses from Student is get & setStudent(), which are public methods. I'd try to remove the friend class Node and the #include "node.h" to see if that makes a difference.