Forum: C++ hjälp

Forum huvudsida -> Programmering -> C++ hjälp

Sidor: 1

Till botten

Calsong 17:56 - 8:e September 2010 | Post #1
Medlem
Inlägg: 4


Skicka PM
Tjenare. Jag har programmerat sen i julas ungefär, programmerat lite av och till, sammanlagt kanske någon månad, inte så där jätteaktivt men nu vill jag försöka ta tag i det igen.

Håller på med ett litet textbaserat spel för repetition så jag ser till att jag kommer ihåg sakerna och visst gör jag det än så länge men det är ett kompilatorfel som jag inte kan fixa nästan. Jag har försökt med allt jag kommit på!

Jag antar att jag måste posta hela koden för att öka chansen att lista ut det så det gör jag, men jag tar bort allt onödigt som långa output-texter och så, så att det inte blir för tråkigt och rörigt.

Mitt kompilatorfel rör objektorienterad programmering.
Detta är mitt kompilatorfel:
1>c:users ilsdocumentsvisual studio 2010projectsallegrofirstmitt textbaserade spelmitt textbaserade spelhuman warrior (definition).cpp(6): error C2011: 'HumanWarrior' : 'class' type redefinition

Detta är min kod

Mitt textbaserade spel.cpp
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <string>
  4. #include "Human Warrior (deklaration).h"
  5.  
  6. using namespace std;
  7.  
  8. void Continue();
  9.  
  10. //Karaktärvals-deklarationer
  11. void raceSelectFunction();
  12. void classSelectFunction();
  13.  
  14. //Karaktärsklassfunktions-deklarationer
  15. void characterClassWarrior();
  16. void characterClassRogue();
  17. void characterClassMage();
  18. void characterClassTempleKnight();
  19. void characterNameClassRaceCity();
  20.  
  21. //infofunktions-deklarationer
  22. void classInfo();
  23.  
  24. //Diverse "cin >>"-deklarationer
  25. int raceSelect;
  26. int classSelect;
  27. int pressAnyKey;
  28. string classSelectAfterClassInfo;
  29. string Select;
  30. string characterName;
  31. string characterCity;
  32.  
  33. int main()
  34. {
  35. raceSelectFunction();
  36. classSelectFunction();
  37. cout << "Now that you've chosen race and class you have to choose a name for your character. What do you want the name of your character to be? ";
  38. cin >> characterName;
  39. system ("CLS");
  40. cout << "Your name is " << characterName << ". ";
  41. cout << "Where are you from? Type in the name of the city that you live in ";
  42. cin >> characterCity;
  43. system ("CLS");
  44. cout << "You are from " << characterCity << ".";
  45. Continue();
  46. characterNameClassRaceCity();
  47. cout << " Let's continue to the actual game";
  48. Continue();
  49.  
  50.  
  51. string quit;
  52. cin >> quit;
  53. return 0;
  54. }
  55. //Ovan är main function
  56.  
  57. void Continue()
  58. {
  59. char c;
  60. cout << " Press any letter-key and press 'enter' to continue ";
  61. cin >> c;
  62. }
  63. //Ovan är funktionen "Continue"
  64.  
  65. void raceSelectFunction()
  66. {
  67. system ("CLS");
  68. cout << "Hi! Welcome to my textbased RPG. Select race and class below. ";
  69. cout <<"Please the race you want to play as. This will give you a skill unique for your class ";
  70. cout << "1. Human ";
  71. cout << "2. Elf ";
  72. cout << "3. Orc ";
  73. cout << "4. Info on the races and their special skills ";
  74. cin >> raceSelect;
  75.  
  76. switch ( raceSelect )
  77. {
  78. case 1:
  79. system ("CLS");
  80. cout << "You chose the human race. ";
  81. break;
  82.  
  83. case 2:
  84. system ("CLS");
  85. cout << "You chose the elf race. ";
  86. break;
  87.  
  88. case 3:
  89. system ("CLS");
  90. cout << "You chose the orc race ";
  91. break;
  92.  
  93. case 4:
  94. system ("CLS");
  95. cout << "Text om raserna";
  96. break;
  97.  
  98. default:
  99. cout << "You didn't choose any of the available options. Please press '1', '2', '3' or '4' on your keyboard";
  100. raceSelectFunction();
  101. break;
  102. }
  103.  
  104. }
  105. //Ovan är raceSelectFunction
  106.  
  107.  
  108. void classSelectFunction()
  109. {
  110. cout << "Now, select the class of your character. ";
  111. cout << "1. Warrior ";
  112. cout << "2. Rogue ";
  113. cout << "3. Mage ";
  114. cout << "4. Temple Knight ";
  115. cout << "5. Info on the classes ";
  116. cout << "Type in the class that you want to select. (1, 2, 3, 4 or 5) ";
  117. cin >> classSelect;
  118.  
  119.  
  120. switch ( classSelect )
  121. {
  122. case 1:
  123. system ("CLS");
  124. characterClassWarrior();
  125. break;
  126.  
  127. case 2:
  128. system ("CLS");
  129. characterClassRogue();
  130. break;
  131.  
  132. case 3:
  133. system ("CLS");
  134. characterClassMage();
  135. break;
  136.  
  137. case 4:
  138. system ("CLS");
  139. characterClassTempleKnight();
  140. break;
  141.  
  142. case 5:
  143. system ("CLS");
  144. classInfo();
  145. break;
  146.  
  147. default:
  148. system ("CLS");
  149. cout << "You didn't choose any of the available options. Please press '1', '2', '3' or '4' on your keyboard ";
  150. classSelectFunction();
  151. break;
  152. }
  153. }
  154. //Ovan är classSelectfunction
  155.  
  156.  
  157. void characterClassWarrior()
  158. {
  159. cout << "You selected the warrior class ";
  160. }
  161. //Ovan är characterClassWarrior-definitionen
  162.  
  163. void characterClassRogue()
  164. {
  165. cout << "You selected the rogue class ";
  166. }
  167. //Ovan är characterClassRogue-definitionen
  168.  
  169. void characterClassMage()
  170. {
  171. cout << "You selected the mage class ";
  172. }
  173. //Ovan är characterClassMage-definitionen
  174.  
  175. void characterClassTempleKnight()
  176. {
  177. cout << "You chose the temple knight class ";
  178. }
  179. //Ovan är characterClassTempleKnight-definitionen
  180. //Ovan är karaktärklassernas definitioner
  181.  
  182.  
  183. void classInfo()
  184. {
  185. system ("CLS");
  186. cout << "Text om klasserna";
  187. cout << "Type in 'Select' to select class ";
  188. cin >> classSelectAfterClassInfo;
  189.  
  190. if ( classSelectAfterClassInfo == Select)
  191. {
  192. classSelectFunction();
  193. }
  194. }
  195. //Ovan är classInfo-funktionen
  196.  
  197. void characterNameClassRaceCity()
  198. {
  199. if ( raceSelect == 1 )
  200. {
  201. system ("CLS");
  202. cout << "You are a human ";
  203.  
  204. if ( classSelect == 1 )
  205. {
  206. cout << "warrior";
  207. cout << " and you will be known as " << characterName << " 'the warrior' of " << characterCity << " ";
  208. HumanWarrior HW1;
  209. HW1.stats();
  210. }
  211.  
  212. if ( classSelect == 2 )
  213. {
  214. Senast redigerad 18:17 - 8:e September 2010


martin310 18:22 - 8:e September 2010 | Post #2
Medlem
Inlägg: 132


Skicka PM
Kan man få se: Human Warrior (deklaration).h?

-------------------------

Hjälp jorden med Granola, det är gratis och funkar på både Windows och Linux, och försämrar inte datorns prestandad!





Calsong 18:23 - 8:e September 2010 | Post #3
Medlem
Inlägg: 4


Skicka PM
Jag ser sidan konstigt och allt jag postade kom inte med, vad har hänt? Är min kod för "bred"?

Här kommer [B]Human Warrior (deklaration).h[B]

  1. class HumanWarrior
  2. {
  3. public:
  4. HumanWarrior();
  5. void stats();
  6. void levelUp();
  7. void afterLvlUp();
  8.  
  9. private:
  10. int health;
  11. int strength;
  12. int agility;
  13. int stamina;
  14. };


Och här [B]Human Warrior (definition).cpp[B]
  1. #include "stdafx.h"
  2.  
  3. #include "Human Warrior (deklaration).h"
  4.  
  5. class HumanWarrior
  6. {
  7. HumanWarrior::HumanWarrior()
  8. {
  9. health = 56;
  10. strength = 27;
  11. agility = 12;
  12. stamina = 40;
  13. }
  14.  
  15. void HumanWarrior::stats()
  16. {
  17. cout << " Your stats are: " << "Health: " << health << " " << "Strength: " << strength << " " << "Agility: " << agility << " " << "Stamina: " << stamina << " ";
  18. }
  19.  
  20. void HumanWarrior::levelUp()
  21. {
  22. health ++;
  23. strength ++;
  24. agility ++;
  25. stamina ++;
  26. cout << "Level Up! ";
  27. }
  28.  
  29. void HumanWarrior::afterLvlUp()
  30. {
  31. cout << "Your stats are now, after level up, Health: " << health << " " << "Strength: " << strength << " " << "Agility: " << agility << " " << "Stamina: " << stamina;
  32. }
  33.  
  34. };



Senast redigerad 18:28 - 8:e September 2010


Calsong 19:04 - 8:e September 2010 | Post #4
Medlem
Inlägg: 4


Skicka PM
Kom på nu att det kanske inte alls rör objektorienterad programmering. Jag som är förvirrad bara... Hade för mig att jag trodde att det inte gick att inkludera Human Warrior (deklaration).h i båda .cpp-filerna.

Så var det inte men det verkar inte gå att redigera för ca halva sidan är osynlig för mig.

Men aja, det verkar ha blivit konstigt när jag postade så glöm tråden, den kan tas bort eller w/e.


Edit: Ok, jag kommer nu ihåg hur frustrerad jag är så jag postar igen, inte så mycket detaljer den här gången. Jag insåg att det var onödigt. Jag postar inte i "källkod-form" för det verkar bara bli problem då.

Mitt textbaserade spel.cpp
#include "stdafx.h"
#include <iostream>
#include <string>
#include "Human Warrior (deklaration).h"

int main()
{
    raceSelectFunction();
    classSelectFunction();
    cout << "Now that you've chosen race and class you have to choose a name for your character. What do you want the name of your character to be? ";
    cin >> characterName;
    system ("CLS");
    cout << "Your name is " << characterName << ". ";
    cout << "Where are you from? Type in the name of the city that you live in ";
    cin >> characterCity;
    system ("CLS");
    cout << "You are from " << characterCity << ".";
    Continue();
    characterNameClassRaceCity();
    cout << " Let's continue to the actual game";
    Continue();
    

string quit;
cin >> quit;
return 0;
}

Human Warrior (deklaration).h
class HumanWarrior
{
public:
    HumanWarrior();
    void stats();
    void levelUp();
    void afterLvlUp();

private:
    int health;
    int strength;
    int agility;
    int stamina;
};

Human Warrior (definition).cpp
#include "stdafx.h"
#include "Human Warrior (deklaration).h"

class HumanWarrior
{ (Här är problemet(?))
    HumanWarrior::HumanWarrior()
    {
        health = 56;
        strength = 27;
        agility = 12;
        stamina = 40;
    }

    void HumanWarrior::stats()
    {
        cout << " Your stats are: " << "Health: " << health << " " << "Strength: " << strength << " " << "Agility: " << agility << " " << "Stamina: " << stamina << " ";
    }

    void HumanWarrior::levelUp()
    {
        health ++;
        strength ++;
        agility ++;
        stamina ++;
        cout << "Level Up! ";
    }

    void HumanWarrior::afterLvlUp()
    {
        cout << "Your stats are now, after level up, Health: " << health << " " << "Strength: " << strength << " " << "Agility: " << agility << " " << "Stamina: " << stamina;
    }

};


Problemet ser ut såhär:
mitt textbaserade spelhuman warrior (definition).cpp(5): error C2011: 'HumanWarrior' : 'class' type redefinition


Senast redigerad 19:37 - 8:e September 2010


ozamosi 11:49 - 9:e September 2010 | Post #5
Administratör
Inlägg: 1129


Skicka PM
Kompilatorn säger att du inte får deklarera klassen i både Human Warrior (definition).cpp och Human Warrior (deklaration).h. Du har skrivit nästan rätt i cpp-filen, förutom att class HumanWarrior inte ska vara med alls. funktionsnamnet *void HumanWarrior::levelUp()* betyder (något förenklat) "hitta en klass HumanWarrior. Det här är den klassens levelUp()-funktion". Så ta bort raden med class HumanWarrior och de tillhörande måsvingarna ur .cpp-filen så ser det bättre ut.

-------------------------
Ljusblå



martin310 15:35 - 9:e September 2010 | Post #6
Medlem
Inlägg: 132


Skicka PM
En tumregel till större projekt är att i alla .H filer ha:

#pragma once

överst!

-------------------------

Hjälp jorden med Granola, det är gratis och funkar på både Windows och Linux, och försämrar inte datorns prestandad!





Calsong 13:38 - 15:e September 2010 | Post #7
Medlem
Inlägg: 4


Skicka PM
Hehe, tack så mycket. Fixade problemet, glömde att skriva det här.

Vi kan glömma det problemet nu. Jag har ett nytt. Jag försöker få SFML att fungera men jag får detta:
SFML-1.6libsfml-system.dll : fatal error LNK1107: invalid or corrupt file: cannot read at 0x2B8
  1. #include <SFML/System.hpp>
  2. #include <iostream>
  3.  
  4. int main()
  5. {
  6. sf::Clock Clock;
  7. while (Clock.GetElapsedTime() < 5.f)
  8. {
  9. std::cout << Clock.GetElapsedTime() << std::endl;
  10. sf::Sleep(0.5f);
  11. }
  12.  
  13. return 0;
  14. }

Jag tror att detta är problemet:
projekt -> properties -> configuration properties -> linker -> input -> additional dependencies: sfml-system.dll;%(AdditionalDependencies)

För när jag ändrar den sista till:
sfml-system.lib;%(AdditionalDependencies)
Så får jag ett runtime error istället: "Det gick inte att starta programmet eftersom sfml-system.dll saknas på datorn. Du kan försöka åtgärda problemet genom att installera om programmet."

Det måste väl betyda att det handlar om något med det jag skrev in där.

Förresten så fick jag skriva "sfml-system.dll" själv. Jag fick inte bläddra bland mina mappar eller något. Ska det vara så?


Kan någon hjälpa mig?


EDIT: Nvm, fixade det. Nu kan jag använda SFML.


Senast redigerad 17:42 - 16:e September 2010


Independence 16:52 - 19:e September 2010 | Post #8
Administratör
Inlägg: 1800


Skicka PM

räksmörgås



-------------------------

Vi är riddarna som säger fiskbulle!





Sidor: 1

Forum huvudsida -> Programmering -> C++ hjälp
Atom feed

Du får inte posta i den här tråden | Till toppen