[英]C++ Interface Compiling
編輯:
我想出了解決方案。 我沒有在編譯指令中添加-combine,而是生成了錯誤。
我正在研究Deitel和Deitel書《 C ++ How to Program》,並遇到了使用g ++構建和編譯C ++接口的問題。 問題是,我已經在.h文件中聲明了該類,並在.cpp文件中定義了實現,但是當我嘗試編譯編寫的測試文件時,我無法弄清楚如何使其能夠正常工作。 我收到的g ++錯誤是:
Undefined symbols:
"GradeBook::GradeBook(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)", referenced from:
_main in ccohy7fS.o
_main in ccohy7fS.o
"GradeBook::getCourseName()", referenced from:
_main in ccohy7fS.o
_main in ccohy7fS.o
ld: symbol(s) not found
collect2: ld returned 1 exit status<
如果有人能指出我正確的方向,我將不勝感激。
我的頭文件:
//Gradebook 6 Header
//Purpose is to be the class declaration for the class Gradebook 6
//Declare public, privates, and function names.
#include //the standard c++ string class library
using std::string;
//define the class gradebook
class GradeBook
{
public: //all the public functions in the class
GradeBook(string ); //constructor expects string input
void setCourseName (string ); //method sets course name--needs string input
string getCourseName(); //function returns a string value
void displayMessage(); //to console
private: //all private members of the class
string courseName;
}; //ends the class declaration
我的.cpp文件是:
//Gradebook 6
// The actual implementation of the class delcaration in gradebook6.h
#include
using std::cout;
using std::endl;
#include "gradebook6.h" //include the class definition
//define the class gradebook
GradeBook::GradeBook(string name) //constructor expects string input
{
setCourseName(name); //call the set method and pass the input from the constructor.
}
void GradeBook::setCourseName (string name) //method sets course name--needs string input
{
courseName = name; //sets the private variable courseName to the value passed by name
}
string GradeBook::getCourseName() //function returns a string value
{
return courseName;
}
void GradeBook::displayMessage() //function does not return anything but displays //message to console
{
cout //message here, the pre tag isn't letting it display
} //end function displayMessage
最后,我編寫了用於實現接口並對其進行測試的測試文件。
// Gradebook6 Test
// Program's purpose is to test our GradeBook5 header file and file seperated classes
#include
using std::cout;
using std::endl;
#include "gradebook6.h" //including our gradebook header from the local file.
//being program
int main()
{
//create two gradebook objects
GradeBook myGradeBook1 ("CSC 101 Intro to C++ Programming"); //create a default object using the default constructor
GradeBook myGradeBook2 ("CSC 102 Data Structures in C++");
//display intitial course name
cout //another output message here that the code tag does not like
return 0;
}
看起來您只需要在GradeBook.cpp對象文件中鏈接到最終的可執行文件即可。 想要發布您的Makefile或構建方式嗎?
您看到的是鏈接器錯誤(指示在“返回的ld”位中:ld是鏈接器)。 您需要將所有.cpp文件提供給g ++,以便它將進行編譯和鏈接,或者使用后者的-c開關從.cpp中創建一個.o,然后在另一個g ++命令上使用THAT .o進行構建(鏈接)可執行文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.