类文件链接
C++ Class file linking
我对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!nn";
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 getBookInfon";
getBookInfo(book1);
cout << "nAfter getBookInfon";
system("pause");
return 0;
}
我确信我不需要在每个文件中包含一个字符串类,但是如果我不这样做,我会得到更多的错误。当我尝试按原样运行它时,我得到一个编译器错误,说明:
错误C2352: 'Book::storeBook':非法调用非静态成员函数
当我注释掉它的调用(实现文件的第38行)并再次运行只是为了看看我是否可以进入getBookInfo函数时,我得到了这个:
1>Library Test Function。objecj: error LNK2005: "public: void __thiscall Book::checkOutBook(void)"(?checkOutBook@Book@@QAEXXZ)已在BookMain.obj中定义
1>Library Test Function。objecj: error LNK2005: "public: void __thiscall Book::displayBookInfo(void)"(?displayBookInfo@Book@@QAEXXZ)已在BookMain.obj中定义
1>Library Test Function。objecj:错误LNK2005: "void __cdecl getBookInfo(类图书&)"(? getbookinfo @@ yaxaavbook @@@ @@ z)已经在BookMain.obj中定义
1>Library Test Function。obj: error LNK2005: "public: void __thiscall Book::storeBook(类std::basic_string,类std::allocator>,类std::basic_string,类std::allocator>,类std::basic_string,类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()给numInStock成员变量加1
•string getTitle()返回title
中的值•int getNumInStock()返回numInStock中的值
2. 创建BookMain.cpp来测试你的BookClass。
功能:
•void getBookInfo (BookClass &);
o要求用户输入一本书的信息,然后调用成员函数storeBook将信息存储在BookClass变量中。
用主程序测试你的类和函数:
提前感谢您的任何建议!
Book::storeBook()
是一个成员函数。它不是一个静态函数,所以这是错误的:
Book::storeBook(bookTitle, authorName, bookPublisher, bookISBN, bookPrice, bookYear, booksInStock);
可以改成book.storeBook(...)
Do not #include"BookMain.cpp"
。这会导致链接错误。您很可能需要清理和重建。
这段代码调用storeBook()就好像它是一个静态方法:
Book::storeBook(...)
但是这个方法实际上被声明为非静态的:
class Book {
...
void storeBook(...)
这将导致您的错误。知道正确的解决方案有点棘手。方法是否应该是静态的?如果该方法在现有的Book实例上操作,那么它应该是非静态的,并且您希望像这样调用它:
book.storeBook(...)
这将调用作为"Book"传递给getBookInfo函数的Book实例的storeBook方法。
根据你的问题描述,我想这就是你想要的。
include "在这两个文件中,main.cpp文件和BookClass.cpp。然后一起编译。假设你正在使用g++
g++ -std=c++11 BookClass.cpp main.cpp -o file.out
不要在main.cpp文件中包含cpp文件,不要在头文件中包含std命名空间。使用完全限定的路径代替->std::无论
- 无法链接文件系统库C++
- 如何在C++中链接 .a 文件
- 在为 Java 编译动态链接文件时,我应该选择哪些 g++ 参数
- Makefile 将在文件更改时链接文件,但不会重新编译更改的源文件
- CMake 从子目录和父目录链接文件
- 如何将GCC链接器固定地址添加到链接文件中
- 在GCC中链接.h文件
- 使用GDB中的多个链接文件进行调试
- visualstudio2010表达了不同项目中的链接文件
- 编译器是否会优化未使用的链接文件
- 尝试取消链接文件时编译 C++ 时出错
- 链接文件错误,VS 2015中的错误LNK2005
- 用C++中的链接文件编译错误
- Makefile未链接.o文件
- 如何链接.h文件中声明为extern的函数,以创建.dll文件
- 通过makefiles链接文件
- 未定义的引用错误,即使在链接c文件之后
- 多个链接c++文件
- 使用CMakeLists.txt链接c++文件
- 在c++中链接文件