C ++:错误'std :: string'是私有的

时间:2015-10-18 22:38:56

标签: c++

因此,对于课堂,我必须编写一个模拟赛马的程序。这是我们第一个涉及课程的重大项目。我被困在接受主题中提到的错误。现在,自从它们被引入以来,我一直在努力使用指针,所以我毫不怀疑这是我的整体问题所在。我已经向我的教授求助了,但他没有回复我的电子邮件。我也联系了朋友等,没有人回复我。

这是我的头文件(Horse.h):

#ifndef HORSE_H
#define HORSE_H
#include <string>

class Horse
{
  private:
    std::string name;
    std::string jockey;
    int maxSpeed;
    int distanceTraveled;
    int racesWon;
  public:
    Horse(std::string, std::string);
    void runOneSecond(int);
    void sendToGate();
    void displayHorse (double);  
};

#endif // HORSE_H

这是我的Horse.cpp:

#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>
#include "Horse.h"
using namespace std;

Horse::Horse(string name, string jockey)
{
  srand (time(NULL));
  int maxSpeed = rand() % 30 + 1;
  distanceTraveled = 0;  
};

void Horse::runOneSecond(int maxSpeed) 
{
  srand (time(NULL));
  distanceTraveled = rand() % maxSpeed;
};

void Horse::sendToGate()
{
  distanceTraveled = 0;  
};

void Horse::displayHorse(double raceDistance)
{
  int percentage;
  for (int i = 0; i < numberOfHorses; i++)
  {
    cout << "|";

  }
};

这是我的main.cpp:

#include <iostream>
#include <string>
#include <cctype>
#include "Horse.h"
using namespace std;

int main()
{
  double raceDistance = 0;
  int numberOfHorses = 0;
  char choice = 'Y';
  string name;
  string jockey;


  cout << "Enter the number of horses for the race: ";
  cin >> numberOfHorses;
  Horse** horsePtr = new Horse* [numberOfHorses];


// Trouble section.   
  for (int i = 0; i < numberOfHorses; i++)
  {
    cout << "Fill in the name of the horse: ";
    cin >> horsePtr[i]->name;
    cout << "Fill in the name of the jockey: ";
    cin >> horsePtr[i]->jockey;
  }

  cout << "How long should the race be (in meters): ";
  cin >> raceDistance;

  cout << endl;
  cout << "Start!" << endl;

  for (int i = 0; i < numberOfHorses; i++)
  {
    horsePtr[i]->sendToGate();
  }

  for (int i = 0; i < numberOfHorses; i++)
  {
    horsePtr[i]->displayHorse(raceDistance);
  }

  cout << "Show the next second of the race? ";
  cin >> choice;

  while(toupper(choice) == 'Y')
  {
    if (toupper(choice) == 'Y')
    {
      for (int i = 0; i < numberOfHorses; i++)
      {
        horsePtr[i]->runOneSecond(maxSpeed);
        horsePtr[i]->displayHorse(raceDistance);
      }
    }
  }

  return 0;
}

这是错误:

Horse.cpp: In member function ‘void Horse::displayHorse(double)’:
Horse.cpp:29:23: error: ‘numberOfHorses’ was not declared in this scope
   for (int i = 0; i < numberOfHorses; i++)
                   ^
In file included from main.cpp:4:0:
Horse.h: In function ‘int main()’:
Horse.h:8:17: error: ‘std::string Horse::name’ is private
     std::string name;
                 ^
main.cpp:25:25: error: within this context
     cin >> horsePtr[i]->name;
                         ^
In file included from main_dmj8t6.cpp:4:0:
Horse.h:9:17: error: ‘std::string Horse::jockey’ is private
     std::string jockey;
             ^
main.cpp:27:25: error: within this context
     cin >> horsePtr[i]->jockey;
                         ^
main.cpp:55:35: error: ‘maxSpeed’ was not declared in this scope
         horsePtr[i]->runOneSecond(maxSpeed);
                                   ^

1 个答案:

答案 0 :(得分:3)

您无法访问private的{​​{1}}成员。在您的示例中,您的class类包含5 Horse

private members

可以在class Horse { private: std::string name; std::string jockey; int maxSpeed; int distanceTraveled; int racesWon; public: }; 类方法和任何private类中访问这些Horse成员;但是:没有其他人可以访问它们。

friend

在标记的两行上,您正在尝试访问int main() { // Trouble section. for (int i = 0; i < numberOfHorses; i++) { cout << "Fill in the name of the horse: "; cin >> horsePtr[i]->name; <-- cout << "Fill in the name of the jockey: "; cin >> horsePtr[i]->jockey; <-- } } private成员,并且编译器不会允许它。

考虑将这些变量Horse设为public,或为它们提供setter函数:

Horse

您正在为class Horse { private: std::string name; std::string jockey; int maxSpeed; int distanceTraveled; int racesWon; public: void SetName(std::string s) { name = s; } void SetJockey(std::string s) { jockey = s; } }; int main() { std::string jockeyName; std::cout << "Enter a name for the jockey: "; std::cin >> jockeyName; Horse* h = new Horse; h->SetJockey(jockeyName); } 提供一个public构造函数,但需要两个Horse(您都不使用它),因此您可以将相关信息传递给std::string

Horse

请注意,我的两个示例只是匹配您当前的代码。您的数组应替换为std::vector,指针(如果必需)应替换为std::unique_ptr(或Horse::Horse(std::string n, std::string j) : name(n), jockey(j) { // Other things... } int main() { Horse* h = new Horse("Phar Lap", "Jim Pike"); } ,以满足您的需求为准。)