繁体   English   中英

在Visual Studio中为函数定义和头文件编译单独的.cpp文件

[英]compile separate .cpp files for function definitions and header files in Visual Studio

我有两个文件,Book.h(头文件)和Book.cpp。 头文件包含Book类的函数头。 我已将功能主体放置在Book.cpp文件中。

我只想编译Book.cpp文件。 当我尝试这样做时,我收到很多错误,说明`重载函数仅在返回类型与' 类型 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

这是我微不足道的主要功能的内容:

#include<iostream>
#include "Book.h" //why won't this work?

using namespace std;

int main(){
    Book myBook;



    return 0;

}

从注释中遵循:在标头中,确保已声明或定义了所有使用的类型,在这种情况下,请包括。 并且,确保可以找到包含的文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM