gpt4 book ai didi

c++ - 从另一个类中访问对象的类方法,C++

转载 作者:行者123 更新时间:2023-11-28 06:11:01 25 4
gpt4 key购买 nike

我正在研究谷歌 C++ 类(class)数据库项目,但遇到了一个难题。我有两个类 Composer 和 Database 用于创建 Composer 对象和一个数据库对象来存储 Composer 数组。这些类在 .h 文件中定义,将包含在最终的 .cpp 文件中。与此同时,我正在对每个类进行测试,每个类都有一个 test.cpp 文件。我的 Composer 类似乎按预期工作,但当我在数据库中创建 Composer 对象时,我的数据库类没有访问 Composer 类的方法。任何帮助是极大的赞赏。文件如下。

Composer 类:

#include <iostream>
using namespace std;

const int kDefaultRanking = 10;

class Composer {

public:
Composer(): first_name_(), last_name_(), composer_yob_(), composer_genre_(),
ranking_(kDefaultRanking), fact_(){}

void set_first_name(string in_first_name){
first_name_ = in_first_name;
}
void set_last_name(string in_last_name){
last_name_ = in_last_name;
}
string get_last_name(){
return last_name_;
}
void set_composer_yob(int in_composer_yob){
composer_yob_ = in_composer_yob;
}
void set_composer_genre(string in_composer_genre){
composer_genre_ = in_composer_genre;
}
void set_ranking(int in_ranking){
ranking_ = in_ranking;
}
void set_fact(string in_fact){
fact_ = in_fact;
}

void Promote(int increment){
ranking_ -= increment;
}
void Demote(int decrement){
ranking_ += decrement;
}
void Display(){
cout << "First Name: " << first_name_ << endl;
cout << "Last Name: " << last_name_ << endl;
cout << "Year of Birth: " << composer_yob_ << endl;
cout << "Genre: " << composer_genre_ << endl;
cout << "Fact: " << fact_ << endl;
cout << "Ranking: " << ranking_ << endl;
}

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

Composer 测试:

#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();
}

数据库类:

#include  <iostream>
#include "Composer.h"

const int kMaxComposers = 100;

class Database {
public:
Database(): composers_(), next_slot_(0){}

Composer& AddComposer(string in_first_name, string in_last_name,
string in_genre, int in_yob, string in_fact){
Composer composer;
composers_[next_slot_] = composer;
composer.set_first_name(in_first_name);
composer.set_last_name(in_last_name);
composer.set_composer_genre(in_genre);
composer.set_composer_yob(in_yob);
composer.set_fact(in_fact);
next_slot_++;
}
Composer& GetComposer(string in_last_name){
for(int i=0;i<kMaxComposers;i++){
if (composers_[i].get_last_name() == in_last_name){
composers_[i].Display();
}
}
}
void DisplayAll(){
for(int i=0;i<kMaxComposers;i++){
if(composers_[i].get_last_name().length() > 0){
composers_[i].Display();
}
}
};
void DisplayByRank();

private:
Composer composers_[kMaxComposers];
int next_slot_;
};

数据库测试:

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

int main() {
Database myDB;

Composer& comp1 = myDB.AddComposer("Ludwig van", "Beethoven", "Romantic", 1770,
"Beethoven was completely deaf during the latter part of his life - he never
heard a performance of his 9th symphony.");
comp1.Promote(7);

Composer& comp2 = myDB.AddComposer("Johann Sebastian", "Bach", "Baroque", 1685,
"Bach had 20 children, several of whom became famous musicians as well.");
comp2.Promote(5);

Composer& comp3 = myDB.AddComposer("Wolfgang Amadeus", "Mozart", "Classical", 1756,
"Mozart feared for his life during his last year - there is some evidence
that he was poisoned.");
comp3.Promote(2);

cout << endl << "all Composers: " << endl << endl;
myDB.DisplayAll();
}

在编译和运行 testdatabase.cpp 时,我得到了 Composer 类的正确显示输出,但是当测试调用 comp1.promote(#) 时,没有任何反应,每个 Composer 的排名打印为 10。

最佳答案

代替

Composer& AddComposer(string in_first_name, string in_last_name, 
string in_genre, int in_yob, string in_fact){
Composer composer;
composers_[next_slot_] = composer;

尝试

Composer& AddComposer(string in_first_name, string in_last_name, 
string in_genre, int in_yob, string in_fact){
Composer &composer = composers_[next_slot_];

为什么会这样 => 这是 C++,而不是 C#,您没有引用 composers[x],而是复制了(目前)空 Composer 的内容。

关于c++ - 从另一个类中访问对象的类方法,C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31277648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com