简体   繁体   English

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

[英]Accessing class method for object from within another class, C++

I am working through the google c++ course database project and have run into a quandary. 我正在研究google c ++课程数据库项目,并且陷入了困境。 I have two classes Composer and Database for creation of composer objects and a database object to store an array of composers in. These classes are defined in .h files to be included in the final .cpp file. 我有两个类用于创建作曲家对象的Composer和Database和一个用于存储作曲家数组的数据库对象。这些类在.h文件中定义,以包含在最终的.cpp文件中。 In the meantime I am working through the testing for each class with a test.cpp file for each. 同时,我正在通过每个类的test.cpp文件来完成每个类的测试。 My composer class appears to be working as intended, but my database class is not accessing the methods from the composer class when i create a composer object in my database. 我的composer类似乎正在按预期工作,但是当我在数据库中创建composer对象时,我的数据库类无法从composer类访问方法。 Any help is greatly appreciated. 任何帮助是极大的赞赏。 The files are as follows. 文件如下。

The composer class: 作曲家课:

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

the composer test: 作曲家测试:

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

the database class: 数据库类:

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

the database test: 数据库测试:

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

When compiling and running testdatabase.cpp I get the proper display output for the composer class, but when the test calls comp1.promote(#) nothing occurs and the ranking prints out as 10 for each composer. 当编译并运行testdatabase.cpp时,我获得了composer类的正确显示输出,但是当测试调用comp1.promote(#)时,什么也没有发生,并且排名显示为每个Composer 10。

Instead of 代替

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;

Try 尝试

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

Why this works => this is C++, not C#, you did not referenced the composers[x], but instead copied the content of the (at the moment) empty Composer. 为何起作用=>这是C ++,而不是C#,您没有引用composers [x],而是复制了(此刻)空Composer的内容。

开设朋友班可能会有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM