【发布时间】: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