C++:错误"std::string"是私有的
C++: error 'std::string' is private
所以,对于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; <--
}
}
在标记的两行中,您试图访问Horse
的private
成员,并且编译器不允许。
考虑将这些变量设为public
到Horse
,或者为它们提供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
,根据你的需要)。
相关文章:
- 使用std::source_location报告错误的最佳实践
- std::is_base_of表示ctor编译错误
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 程序崩溃并显示"std::out_of_range"错误
- 在c++中尝试对对象数组进行排序时,出现std:bad_alloc错误
- 我收到以下错误:抛出'std::bad_alloc'实例后终止调用
- 尝试使用 std::vector<std::thread时出现静态断言失败错误>
- 错误 C2679:二进制"<<":未找到采用类型 'std::string_view' 的右侧操作数的运算符(或者没有可接受的转换)
- 为什么 std::绑定错误参数可以成功?
- std::cout输出int时出现编译错误
- 使用静态库与 std::jthread (g++-10) 的分段错误
- std::unordered_map 类型对象声明期间出现"field has incomplete type"错误
- 在缺少函数重载时抛出异常,并带有 std::variant 而不是编译时错误
- 使用 memcpy() 复制到 std::chrono::milliseconds 会给出错误 -Werror=clas
- 显式 std::exception_ptr 转换为 bool 不存在.VS2010 错误?
- "random_shuffle":不是"std"错误的成员
- VC++ 2010 "array is not a member of std"错误
- 开发C++ "to_string is not a member of std"错误
- STD未来异常-已检索,STD错误
- 交叉编译c++的树莓派std错误