C++接口(interface)编译

标签 c++ compiler-construction interface g++

编辑:

我找到了解决方案。我没有将 -combine 添加到我的编译指令中,这导致了错误。


我正在阅读 Deitel 和 Deitel 的书 C++ How to Program,并且遇到了使用 g++ 构建和编译 C++ 接口(interface)的问题。问题是,我已经在 .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

最后是我写的实现接口(interface)的测试文件,测试一下。


// 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 或您构建它的方式吗?

关于C++接口(interface)编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/880495/

相关文章:

c# - 通用接口(interface)具体类型之间没有隐式引用转换错误

angular - 有什么方法可以在 Angular Material 表中动态添加列?

c++ - 获取错误 : 'mutex' in namespace 'std' does not name a type in MinGW mysys prompt

c++ - 递归幂函数

c++ - 在 cstring 中搜索运算符

java - 如何在Java中实现接口(interface)方法

c++ - 如果库 dosnt 存在于 usr/lib 下,我得到一个错误库未找到

parsing - 通过语法解析为 AST(或 .y+.lang => xml)的工具

algorithm - UML 工具的代码生成功能背后的技术是什么

c - 编译器如何在基本 ANSI C 类型和所选处理器之间建立链接