用头编译 我正在使用Ubuntu学习谷歌开发者网站中的C++。我正在做:

用头编译 我正在使用Ubuntu学习谷歌开发者网站中的C++。我正在做:,c++,ubuntu,header,compilation,g++,C++,Ubuntu,Header,Compilation,G++,这些文件是: // 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 { publ

这些文件是:

// 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_;
};


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

可能还有另一个cpp文件
composer.cpp
左右。你必须把它们联系在一起。您可以通过以下方式实现:

g++ test_composer.cpp composer.cpp

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

您好,欢迎光临。尝试搜索错误消息:
未定义引用
。您需要阅读并学到很多东西。
g++
的程序参数顺序很重要(您还应该添加
-Wall-g
),并且您缺少很多库。我认为练习的一点是,您必须实现
构造函数中的方法。您显示的代码不应该编译(严格地说,它无法链接)。
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
g++ test_composer.cpp composer.cpp