首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C++接口编译

C++接口编译
EN

Stack Overflow用户
提问于 2009-05-19 00:50:20
回答 2查看 956关注 0票数 0

编辑:

我想出了解决办法。我没有将-combine添加到编译指令中,这会产生错误。

我正在阅读Deitel和Deitel的“如何编程”( C++ How to Program )一书,并在使用g++构建和编译C++界面时遇到了问题。问题是,我已经在.h文件中声明了类,并在.cpp文件中定义了实现,但是当我试图编译我编写的测试文件时,我想不出如何让它编译和工作。我收到的g++错误是:

代码语言:javascript
复制
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<

如果有人能给我指明正确的方向,我会感激的。

我的头文件:

代码语言:javascript
复制
//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文件是:

代码语言:javascript
复制
//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

最后,我编写的测试文件实现了接口并对其进行了测试。

代码语言:javascript
复制
// 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;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2009-05-19 00:52:12

看起来,您只需将GradeBook.cpp对象文件中的链接链接到最终的可执行文件。介意张贴您的makefile或您正在构建它的方式?

票数 1
EN

Stack Overflow用户

发布于 2009-05-19 01:08:20

您所看到的是链接器错误(此指示位于"ld返回“位: ld是链接器)。您需要将所有.cpp文件交给g++,以便编译和链接它们,或者使用后者的-c开关从.cpp生成.o,然后在进一步的g++命令上使用.o构建(链接)可执行文件。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/880495

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档