Reputation: 23
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.
Upvotes: 0
Views: 376
Reputation: 1096
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.
Upvotes: 1
Reputation: 21803
The problem is that student.h includes node.h, which needs the full definition of Student, so it includes student.h, but as we are already processing student.h the header guards have been defined and nothing is there, so it continues in node.h and gets to a Student variable, but it hasn't been defined yet, because even though we started with student.h, we never actually got to the Student class.
The solution is to remove the include for node.h from student.h
Upvotes: 2
Reputation: 1096
I'm deeply confused by the two lines:
#include "student.h"
class Student;
I don't believe the second is necessary.
What this is doing is saying "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" immediately followed by "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". The compiler is correctly confused.
Upvotes: 1