【发布时间】:2014-11-29 21:04:21
【问题描述】:
我有两个文件,Book.h(头文件)和 Book.cpp。头文件包含我的 Book 类的函数头。我已将函数体放置在我的 Book.cpp 文件中。
我只是想编译 Book.cpp 文件。当我尝试这样做时,我收到很多错误,说“重载函数仅因返回类型与 'type Book::functionName(void)
不同我已将头文件包含在我的 Book.cpp 文件中,如下所示。我不明白缺少什么。我在网上搜索过,但每个编译器似乎处理编译多个文件的方式完全不同。
注意:我还尝试添加另一个名为 menu.cpp 的 .cpp,其中包含我的主目录。在这篇主要文章中,我只是尝试实例化一个 book 对象,但编译器无法识别我的 Book 类或它的任何成员函数......
以下是 Book.cpp 的内容:
#include "Book.h"
#include <string>
using namespace std;
static const int CHECK_OUT_LENGTH = 21;
Book::Book(){
idCode = "";
title = "";
author = "";
};
Book::Book(std::string idc, std::string t, std::string a){
idCode = idc;
title = t;
author = a;
};
int Book::getCheckOutLength(){
return CHECK_OUT_LENGTH;
};
std::string Book::getIdCode(){
return idCode;
};
std::string Book::getTitle(){
return title;
};
std::string Book::getAuthor(){
return author;
};
Locale Book::getLocation(){
return location;
};
void Book::setLocation(Locale lo){
location = lo;
};
Patron* Book::getCheckedOutBy(){
return checkedOutBy; //will return the address of the current patron who has checked out the book
};
void Book::setCheckedOutBy(Patron* p){
checkedOutBy = p; //will set the address of checkedOutBy pointer to the adress of the pointer p
};
Patron* Book::getRequestedBy(){
return requestedBy;
};
void Book::setRequestedBy(Patron* p){
requestedBy = p;
};
int Book::getDateCheckedOut(){
return dateCheckedOut;
};
void Book::setDateCheckedOut(int d){
dateCheckedOut = d;
};
以下是 Book.h 的内容:
#ifndef examples_Book_h
#define examples_Book_h
class Patron;
enum Locale {ON_SHELF, ON_HOLD, CHECKED_OUT};
class Book
{
private:
std::string idCode;
std::string title;
std::string author;
Locale location;
Patron* checkedOutBy;
Patron* requestedBy;
int dateCheckedOut;
public:
static const int CHECK_OUT_LENGTH = 21;
Book();
Book(std::string idc, std::string t, std::string a);
int getCheckOutLength();
std::string getIdCode();
std::string getTitle();
std::string getAuthor();
Locale getLocation();
void setLocation(Locale lo);
Patron* getCheckedOutBy();
void setCheckedOutBy(Patron* p);
Patron* getRequestedBy();
void setRequestedBy(Patron* p);
int getDateCheckedOut();
void setDateCheckedOut(int d);
};
#endif
以下是我微不足道的 main 函数的内容:
#include<iostream>
#include "Book.h" //why won't this work?
using namespace std;
int main(){
Book myBook;
return 0;
}
【问题讨论】:
-
book.h 使用 std::string 所以应该包含
-
我无法立即发现问题,如果您右键单击 te book.cpp 文件中 book.h 的包含并选择打开它,是否打开了正确的文件? (我想知道你是否有一个旧 book.h 文件潜伏在某处)
-
请复制并粘贴您在答案末尾遇到的前几个错误
-
是的,当我右键单击 Book.h 的包含时,它会打开正确的 Book.h 文件。我还在 Book.h 的顶部添加并包含了
。现在我得到的错误要少得多,这很好,但编译仍然失败。现在这是我收到的唯一错误,这没有任何意义,因为我右键单击它时打开了正确的 Book.h 文件: c:\users\john d\documents\visual studio 2012\projects\project35\project35\menu.cpp(2): fatal error C1083: Cannot open include file: 'Book.h': No such file or directory -
仔细检查 - 如果您右键单击 menu.cpp 中的包含,是否会打开正确的文件?
标签: c++ visual-studio visual-studio-2012