2015-10-18 60 views
-1

因此,对于课程,我必须编写一个模拟赛马的程序。这是我们第一个涉及课程的重大项目。我被卡在主题中提到的错误。现在,自引入它们以来,我一直在与指针进行斗争,所以我毫不怀疑这是我的总体问题所在。我已经请教过我的教授,但他没有回复我的电子邮件。我也联系过朋友等,没有人回到我身边。C++:错误'std :: string'是私人的

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

你应该只调用'srand'一次。你也可以找到'std :: vector '比指针更容易使用。 'cin >> horsePtr [i] - > name;'这是不正确的,因为你还没有在那里分配任何Horse对象。 –

+0

'我<&numberOfHorses'你为什么要拿着'numberOfHorses'的地址? – Adam

回答

3

您不能访问classprivate成员。在你的例子,你的Horse类包含5 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; <-- 
    } 
} 

在两行标记,您试图访问的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); 
} 

您提供public构造函数Horse采用两个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"); 
} 

请注意,我的两个例子都只是符合你的c urrent代码。如果需要,应将其替换为std::vector和指针(或std::shared_ptr,以符合您的需求为准)。

相关问题