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

C++: error 'std::string' is private

本文关键字:std 错误 C++ string      更新时间:2023-10-16

所以,对于class,我必须编写一个模拟赛马的程序。这是我们第一个涉及课堂的大项目。我收到了主题中提到的错误。现在,自从指针被引入以来,我一直在与之斗争,所以我毫不怀疑这就是我的整体问题所在。我向我的教授寻求帮助,但他不回我的邮件。我还联系了朋友等人,但没有人回复我。

这是我的头文件(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

这是我的马。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);
                                   ^

不能访问class下的private成员。在您的示例中,Horse类包含5个private members:

class Horse
{
  private:
    std::string name;
    std::string jockey;
    int maxSpeed;
    int distanceTraveled;
    int racesWon;
public:
};

这些private成员可以在Horse类方法中访问,也可以被任何friend类访问;但是: else不能访问它们。

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

在标记的两行中,您试图访问Horseprivate成员,并且编译器不允许。

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

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

您正在为Horse提供public构造函数,该构造函数需要两个std::string(您都没有使用),因此您可以将相关信息传递给Horse:

Horse::Horse(std::string n, std::string j) : name(n), jockey(j)
{
     // Other things...
}
int main()
{
    Horse* h = new Horse("Phar Lap", "Jim Pike");
}

请注意,我的两个例子只是匹配您当前的代码。你的数组应该替换为std::vector和指针,如果需要,应该替换为std::unique_ptr(或std::shared_ptr,根据你的需要)。