【问题标题】:Compile with header使用头文件编译
【发布时间】:2013-10-18 07:25:18
【问题描述】:

我正在使用 ubuntu 在 Google Developers 网站上学习 C++。我正在做:database project

文件是:

// composer.h, Maggie Johnson
// Description: The class for a Composer record.
// The default ranking is 10 which is the lowest possible.
// Notice we use const in C++ instead of #define.
const int kDefaultRanking = 10;

class Composer {
 public:
 // Constructor
 Composer();
 // Here is the destructor which has the same name as the class
 // and is preceded by ~. It is called when an object is destroyed
 // either by deletion, or when the object is on the stack and
 // the method ends.
 ~Composer();

 // Accessors and Mutators
 void set_first_name(string in_first_name);
 string first_name();
 void set_last_name(string in_last_name);
 string last_name();
 void set_composer_yob(int in_composer_yob);
 int composer_yob();
 void set_composer_genre(string in_composer_genre);
 string composer_genre();
 void set_ranking(int in_ranking);
 int ranking();
 void set_fact(string in_fact);
 string fact();

 // Methods
 // This method increases a composer's rank by increment.
 void Promote(int increment);
 // This method decreases a composer's rank by decrement.
 void Demote(int decrement);
 // This method displays all the attributes of a composer.
 void Display();

 private:
 string first_name_;
 string last_name_;
 int composer_yob_; // year of birth
 string composer_genre_; // baroque, classical, romantic, etc.
 string fact_;
 int ranking_;
};

// test_composer.cpp, Maggie Johnson
//
// This program tests the Composer class.

#include <iostream>
#include "Composer.h"
using namespace std;

int main ()
{
 cout << endl << "Testing the Composer class." << endl << endl;

 Composer composer;

 composer.set_first_name("Ludwig van");
 composer.set_last_name("Beethoven");
 composer.set_composer_yob(1770);
 composer.set_composer_genre("Romantic");
 composer.set_fact("Beethoven was completely deaf during the latter
 part of his life - he never heard a performance of his 9th symphony.");
 composer.Promote(2);
 composer.Demote(1);
 composer.Display();
}

但是当我编译 test_composer.cpp 我有这个问题:

g++ test_composer.cpp

/tmp/cc4M3FJN.o: In function `main':
test_composer.cpp:(.text+0x47): undefined reference to `Composer::Composer()'
test_composer.cpp:(.text+0x7b): undefined reference to `Composer::set_first_name(std::string)'
test_composer.cpp:(.text+0xc7): undefined reference to `Composer::set_last_name(std::string)'
test_composer.cpp:(.text+0xf0): undefined reference to `Composer::set_composer_yob(int)'
test_composer.cpp:(.text+0x124): undefined reference to `Composer::set_composer_genre(std::string)'
test_composer.cpp:(.text+0x170): undefined reference to `Composer::set_fact(std::string)'
test_composer.cpp:(.text+0x199): undefined reference to `Composer::Promote(int)'
test_composer.cpp:(.text+0x1aa): undefined reference to `Composer::Demote(int)'
test_composer.cpp:(.text+0x1b6): undefined reference to `Composer::Display()'
test_composer.cpp:(.text+0x1c2): undefined reference to `Composer::~Composer()'
test_composer.cpp:(.text+0x263): undefined reference to `Composer::~Composer()'
collect2: error: ld returned 1 exit status

我是这个网站的新手,我一直在寻找有关“使用标头编译”的其他问题,但我无法解决。感谢您的支持和帮助。

【问题讨论】:

  • 您好,欢迎您。尝试搜索错误消息:undefined reference
  • 您需要大量阅读和学习。 g++ 的程序参数顺序很重要(您还应该添加 -Wall -g),并且您缺少很多库。
  • 我认为练习的一个要点是您必须实现 Constructors 类中的方法。您显示的代码不应该编译(严格来说它无法链接)。

标签: c++ ubuntu header compilation g++


【解决方案1】:

可能还有另一个cpp文件composer.cpp左右。您必须将它们链接在一起。您可以通过以下方式做到这一点:

g++ test_composer.cpp composer.cpp

但是当您获得更多代码和更多文件时,您可能希望查看 CMake 之类的解决方案。或者大多数 IDE(例如 Eclipse)也可以为您执行此操作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-08
    • 2010-11-25
    • 1970-01-01
    • 2010-11-20
    相关资源
    最近更新 更多