【问题标题】:C++ Class file linkingC++ 类文件链接
【发布时间】:2015-10-10 06:19:38
【问题描述】:

我对 C++ 还是很陌生,在制作类文件并将其链接到我的主程序和实现文件时遇到了麻烦。

这是我的规范文件:

#ifndef BOOKCLASS_H
#define BOOKCLASS_H
#include<string>

using namespace std;

class Book
{
    private:
        string title;       //A string that holds the book’s title
        string author;      //A string that holds the book’s author
        string publisher;   //A string that holds the book’s publisher
        string isbn;        //A string that holds the book’s ISBN number
        double price;       //A double that holds the book’s price
        int year;           //An int that holds the year when the book was published
        int numInStock;     //An int that holds the number of copies of this book
    public:
        Book();                     //constuctor ---- will be overwritten by storeBook if getBookInfo operates properly
        void getBookInfo(Book &);   //gets the information for the book and calls storeBook within to store information to the variables
        void storeBook(string bookTitle, string authorName, string bookPublisher, string bookISBN, double bookPrice, int bookYear, int booksInStock);
        void displayBookInfo();     //Displays the contents of the BookClass member variables
        void checkOutBook();        //Subtracts 1 from the numInStock member variable, tests to make sure numInStock is not 0
        void returnBook()           //Adds 1 to the numInStock member variable
        { numInStock++; };
        string getTitle()           //Returns the value in title
        { return title; };
        int getNumInStock()         //Returns the value in numInStock
        { return numInStock; };
};
#endif // !BOOKCLASS_H

这里是实现文件:

#include<iostream>
#include<string>
#include"BookClass.h"

using namespace std;


//*********************************************************************************************************************************************************************
//constuctor - this constuctor will assign generic values to the class, used primarily for troubleshooting and will be overwritten if program operates properly
//*********************************************************************************************************************************************************************
Book::Book()
{
    std::cout << "Made it into the constructor!\n\n";
    title = "Empty";
    author = "Empty";
    publisher = "Empty";
    isbn = "Empty";
    price = 0.00;
    year = 0000;
    numInStock = 0;
}

//*********************************************************************************************************************************************************************
//Asks the user to enter information for one book, then invokes member function storeBook to store the information in the BookClass variable.
//*********************************************************************************************************************************************************************
void Book::getBookInfo(Book &book)
{
    string bookTitle, authorName, bookPublisher, bookISBN;
    double bookPrice;
    int bookYear, booksInStock;

    cout << "Enter the book title: ";
    getline(cin, bookTitle);
    cout << "Enter the author: ";
    getline(cin, authorName);
    cout << "Enter the publisher: ";
    getline(cin, bookPublisher);
    cout << "Enter the ISBN-10 including dashes(ex. 0-00-000000-0): ";
    getline(cin, bookISBN);
    cout << "Enter the price: ";
    cin >> bookPrice;
    cout << "Enter the year: ";
    cin >> bookYear;
    cout << "Enter the quantity in stock: ";
    cin >> booksInStock;
    Book::storeBook(bookTitle, authorName, bookPublisher, bookISBN, bookPrice, bookYear, booksInStock);
}

//*********************************************************************************************************************************************************************
//Stores the values taken from the user input into the class variables
//*********************************************************************************************************************************************************************
void Book::storeBook(string bookTitle, string authorName, string bookPublisher, string bookISBN, double bookPrice, int bookYear, int booksInStock)
{
    title = bookTitle;
    author = authorName;
    publisher = bookPublisher;
    isbn = bookISBN;
    price = bookPrice;
    year = bookYear;
    numInStock = booksInStock;
}

//*********************************************************************************************************************************************************************
//Displays the contents of the BookClass member variables
//*********************************************************************************************************************************************************************
void Book::displayBookInfo()
{
    cout << "Title: "
         << title
         << "\nAuthor: "
         << author 
         << "\nPublisher: "
         << publisher
         << "\nISBN-10: "
         << isbn
         << "\nPrice: "
         << price
         << "\nYear: "
         << year
         << "\nQuantity in stock: "
         << numInStock;
}

//*********************************************************************************************************************************************************************
//Subtracts 1 from the numInStock member variable, tests to make sure numInStock is not 0
//*********************************************************************************************************************************************************************
void Book::checkOutBook()
{
    if(numInStock <= 0)
    {
        cout << "ERROR: All copies are checked out. Please select another title.\n";
        return;
    }
    else
        numInStock--;
}

最后是我的主程序:

#include"BookClass.h"
#include"BookMain.cpp"

using namespace std;

int main()
{
    Book book1;
    cout << "Before getBookInfo\n";
    getBookInfo(book1);
    cout << "\nAfter getBookInfo\n";

    system("pause");
    return 0;
}

我确信我不需要在每个文件中包含一个字符串类,但如果我不这样做,我会得到更多错误。当我尝试按原样运行它时,我得到一个编译器错误说明:

error C2352: 'Book::storeBook' : 非法调用非静态成员函数

当我注释掉对它的调用(实现文件的第 38 行)并再次运行以查看是否可以进入 getBookInfo 函数时,我得到以下信息:

1>Library Test Function.obj : error LNK2005: "public: void __thiscall Book::checkOutBook(void)" (?checkOutBook@Book@@QAEXXZ) 已在 BookMain.obj 中定义

1>Library Test Function.obj : error LNK2005: "public: void __thiscall Book::displayBookInfo(void)" (?displayBookInfo@Book@@QAEXXZ) 已在 BookMain.obj 中定义

1>Library Test Function.obj : error LNK2005: "void __cdecl getBookInfo(class Book &)" (?getBookInfo@@YAXAAVBook@@@Z) 已在 BookMain.obj 中定义

1>库测试 Function.obj : 错误 LNK2005: "public: void __thiscall Book::storeBook(class std::basic_string,class std::allocator >,class std::basic_string,class std::allocator >,类 std::basic_string,类 std::allocator >,类 std::basic_string,类 std::allocator >,double,int,int)" (?storeBook@Book@@QAEXV?$basic_string@DU?$char_traits@ D@std@@V?$allocator@D@2@@std@@000NHH@Z) 已经在 BookMain.obj 中定义

我知道传递给 getBookInfo 函数的引用变量没有被使用(老师希望函数传递那个参数),主要是因为我不确定如何处理它,但我没有看到是我的错误的问题。有人可以告诉我我做错了什么吗?这是作业的一部分,包括 getBookInfo 函数的格式,我应该将它与实现文件的其余部分分开吗?

会员功能
• void storeBook(string bookTitle, string authorName, string bookPublisher, string bookISBN, double bookPrice, int bookYear, int booksInStock)
o 将参数存储到 BookClass 成员变量中
• void displayBookInfo() 显示 BookClass 成员变量的内容
• void checkOutBook() 从 numInStock 成员变量中减去 1;测试以确保 numInStock 不为​​ 0
• void returnBook() 将 1 加到 numInStock 成员变量
• string getTitle() 返回标题中的值
• int getNumInStock() 返回 numInStock
中的值 2. 创建 BookMain.cpp 来测试您的 BookClass。
功能:
• void getBookInfo (BookClass &);
o 要求用户输入一本书的信息,然后调用成员函数 storeBook 将信息存储在 BookClass 变量中。
用主程序测试你的类和函数:

提前感谢您的任何建议!

【问题讨论】:

    标签: c++ class


    【解决方案1】:

    Book::storeBook() 是一个成员函数。它不是静态函数,所以这是错误的:

    Book::storeBook(bookTitle, authorName, bookPublisher, bookISBN, bookPrice, bookYear, booksInStock);

    可以改为book.storeBook(...)

    不要#include"BookMain.cpp"。这导致您的链接错误。您很可能需要清理和重建。

    【讨论】:

    • 哇,我花了几个小时试图弄清楚 getBookInfo 函数调用,只是发现我错过了原型中的 Book:: .....感谢您的帮助,我不敢相信我错过了那个简单的细节。
    【解决方案2】:

    这段代码调用 storeBook() 就好像它是一个静态方法:

    Book::storeBook(...)
    

    但该方法实际上是声明为非静态的:

    class Book {
        ...
        void storeBook(...)
    

    这是导致您的错误的原因。知道正确的解决方案有点棘手。该方法是否应该是静态的?如果该方法对现有的 Book 实例进行操作,那么它应该是非静态的,您需要这样调用它:

    book.storeBook(...)
    

    这将调用作为“book”传递给 getBookInfo 函数的 Book 实例的 storeBook 方法。

    根据您对问题的描述,我认为这就是您想要的。

    【讨论】:

      【解决方案3】:

      在 main.cpp 文件和 BookClass.cpp 中都包含“BookClass.h”。然后一起编译。假设您正在使用 g++

      g++ -std=c++11 BookClass.cpp main.cpp -o file.out
      

      不要在 main.cpp 文件中包含 cpp 文件,在头文件中包含 std 命名空间。改用完全限定路径 -> std::whatever

      【讨论】:

      • 错误信息表明 OP 使用 Visual C++。为什么是 C++11?该代码不使用它的任何功能。
      • @Melebius 因为大多数人现在使用 C++11。它甚至是最近编译器的默认值。
      • @Melebius 我使用 Visual Studios 2012,因为这是课程所需要的
      猜你喜欢
      • 1970-01-01
      • 2012-09-27
      • 1970-01-01
      • 2015-10-28
      • 1970-01-01
      • 2017-01-04
      • 2015-01-13
      相关资源
      最近更新 更多