alle mine strenge siger at der er fejl!
JEG ER LIGE BEGYNDT PÅ C++ EFTER EN LÆNGER PAUSE MED JAVA. OG JEG ER BEGYNDT AT LAVE ET LILLE TEKSTBASERET SPIL, (crap opdagede ikke capslock :P) det hele virkede også fint indtil jeg lavede den constuctor til klassen monster, hvor indholdet hentes fra en fil. det er dog ikke konstruktøren der er noget i vejen med, en masse sammensatte strenge.#include <iostream>
#include <cstring>
#include <fstream>
#include <cstdlib>
#include <cstdio>
#define ae = '\221'
#define AE = '\222'
#define aa = '\206'
#define AA = '\217'
#define oe = '\233'
#define OE = '\235'
using namespace std;
inline void sTop (void)
{
cout << "N" ae "ste (enter) ";
cin.getline (new char, 0);
}
bool jaNej (void)
{
bool ok = false;
char svar [1];
while (!ok)
{
cout << "(j/n)? ";
cin.getline (svar, 99);
if (svar [0] == 'j')
{
return (true);
} else if (svar [0] == 'n')
{
return (false);
}
cout << "Forket svar... pr" oe "v igen ";
}
}
class monster
{
private:
int liv, forsvar, skade, bonus;
string navn;
public:
monster (string);
monster (string,int,int,int,int);
string toString (void)
{
return ("Navn:\t" navn "\nLiv:\t" liv "\nForsvar\t" forsvar "\nSkade\t" (skade + (bonus / 2)) "\n");
}
};
monster::monster (string navn)
{
char input [10];
int counter = 0;
ifstream monsterInd (navn ".mon");
if (monsterInd.is_open ())
{
while (!monsterInd.eof ())
{
counter++;
monsterInd.getline(input, 10)
switch (counter)
{
case 1:
liv = atoi (input);
break;
case 2:
forsvar = atoi (input);
break;
case 3:
skade = atoi (input);
break;
case 4:
bonus = atoi (input);
}
}
}
}
monster::monster (string Navn, int Liv, int Forsvar, int Skade, int Bonus)
{
navn = Navn;
liv = Liv;
forsvar = Forsvar;
skade = Skade;
bonus = Bonus;
forsvar = Forsvar;
}
monster monster1("ulv");
int main()
{
cout << "Tekst ud til konsollen...\n";
if (jaNej ())
{
cout << "JA - HEPHEY!\n";
} else
{
cout << "NEJ - AAARRRG!!\n";
}
cout << monster1.toString ();
sTop ();
}
Den brokker sig ved alle sammensatte strenge, på trods af at de virkede inden jeg lavede monster(string) konstruktøren.
kompiler loggen ser sådan ud:
Kompiler: Default compiler
Eksekverer g++.exe...
g++.exe "G:\JEK\Andet\c++\program.cpp" -o "G:\JEK\Andet\c++\program.exe" -I"C:\programmer\Dev-Cpp\include\c++" -I"C:\programmer\Dev-Cpp\include\c++\mingw32" -I"C:\programmer\Dev-Cpp\include\c++\backward" -I"C:\programmer\Dev-Cpp\include" -L"C:\programmer\Dev-Cpp\lib"
G:/JEK/Andet/c++/program.cpp: In function `void sTop()':
G:/JEK/Andet/c++/program.cpp:20: no match for `std::basic_ostream<char,
std::char_traits<char> >& = char' operator
C:/programmer/Dev-Cpp/include/c++/iosfwd:61: candidates are:
std::basic_ostream<char, std::char_traits<char> >& std::basic_ostream<char,
std::char_traits<char> >::operator=(const std::basic_ostream<char,
std::char_traits<char> >&)
G:/JEK/Andet/c++/program.cpp:20: parse error before string constant
G:/JEK/Andet/c++/program.cpp: In function `bool jaNej()':
G:/JEK/Andet/c++/program.cpp:39: no match for `std::basic_ostream<char,
std::char_traits<char> >& = char' operator
C:/programmer/Dev-Cpp/include/c++/iosfwd:61: candidates are:
std::basic_ostream<char, std::char_traits<char> >& std::basic_ostream<char,
std::char_traits<char> >::operator=(const std::basic_ostream<char,
std::char_traits<char> >&)
G:/JEK/Andet/c++/program.cpp:39: parse error before string constant
G:/JEK/Andet/c++/program.cpp: In member function `std::string
monster::toString()':
G:/JEK/Andet/c++/program.cpp:53: parse error before `;' token
G:/JEK/Andet/c++/program.cpp: In constructor
`monster::monster(std::basic_string<char, std::char_traits<char>,
std::allocator<char> >)':
G:/JEK/Andet/c++/program.cpp:61: parse error before string constant
G:/JEK/Andet/c++/program.cpp:62: request for member `is_open' in `
monsterInd(...)', which is of non-aggregate type `std::ifstream ()(...)'
G:/JEK/Andet/c++/program.cpp:64: request for member `eof' in `monsterInd(...)',
which is of non-aggregate type `std::ifstream ()(...)'
G:/JEK/Andet/c++/program.cpp:67: request for member `getline' in `
monsterInd(...)', which is of non-aggregate type `std::ifstream ()(...)'
G:/JEK/Andet/c++/program.cpp:68: parse error before `switch'
G:/JEK/Andet/c++/program.cpp:73: case label `2' not within a switch statement
G:/JEK/Andet/c++/program.cpp:76: case label `3' not within a switch statement
G:/JEK/Andet/c++/program.cpp:79: case label `4' not within a switch statement
G:/JEK/Andet/c++/program.cpp: At global scope:
G:/JEK/Andet/c++/program.cpp:84: parse error before `}' token
Eksekvering afbrudt