用标题编译

时间:2013-10-18 07:25:19

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

我正在使用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

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

1 个答案:

答案 0 :(得分:0)

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

g++ test_composer.cpp composer.cpp

但是当你获得更多代码和更多文件时,你可能想看看像CMake这样的解决方案。或者大多数IDE(例如Eclipse)也为您做到这一点。