Permalänk

Construktor problem?? C++

Hejsan, har ett problem och jag tror att det är ett Constructor problem... är fortfarande nybörjare.
Här har ni koden som jag inte får fungera.. Hade uppskattat lite hjälp

#include <iostream> #include <conio.h> #include <Windows.h> using namespace std; enum Color // 1byte/8 bits of info, 128 = bgIntensity, 64 = bgRed, 32 = bgGreen, 16 = bgBlue, 8 = frIntensity, 4 = frRed, 2 = frGreen, 1 = frBlue, 0 = none { NONE = 0, BLUE = 1, GREEN = 2, TEAL = 3, RED = 4, lightTEAL = 11, lightBLUE = 9, lightGREEN = 10, lightRED = 12 }; void MoveCursor(int y, int x) { HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); //Points to what console window that is currently beeing used COORD coord; //?? coord.X = x; coord.Y = y; SetConsoleCursorPosition(handle, coord); //?? } void SetColor(Color forecolor, Color backcolor) //pass color enums { HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(handle, (WORD)forecolor + ((WORD)backcolor << 4)); //Shift the 4 backcolor bits to left } class Map //group togeather relevant variables/functions, private by default { public: Map(){} Map(int width, int height) //Construct { mapWidth = width; mapHeight = height; map = new char*[height]; int heightCreated = 0; int widthCreated = 0; char toWrite; while (heightCreated < height) { map[heightCreated] = new char[width]; widthCreated = 0; while (widthCreated < width) { if (heightCreated == 0 || heightCreated == mapHeight - 1) { toWrite = 'X'; } else if (widthCreated == 0 || widthCreated == mapWidth - 1) { toWrite = 'X'; } else { toWrite = ' '; } map[heightCreated][widthCreated] = toWrite; widthCreated = widthCreated + 1; } heightCreated = heightCreated + 1; } } private: int mapWidth; int mapHeight; char **map; public: void Draw(int yPos, int xPos, char object) { MoveCursor(0, 0); SetColor(Color::NONE, Color::TEAL); //Use :: for clarification. Called Scope when using :: int mapHeigthPrinted = 0; int mapWidthPrinted = 0; while (mapHeigthPrinted < mapHeight) { mapWidthPrinted = 0; while (mapWidthPrinted < mapWidth) { cout << map[mapHeigthPrinted][mapWidthPrinted]; mapWidthPrinted = mapWidthPrinted + 1; } cout << endl; mapHeigthPrinted++; } SetColor(Color::lightGREEN, Color::NONE); //Use :: for clarification. Called Scope when using :: cout << "Coordinates " << endl << "X: " << xPos << " " << endl << "Y: " << yPos << " " << endl; cout << "Object: " << object << endl; } void DrawObject(int y, int x, char object) { map[y][x] = object; } char GetObjectAt(int y, int x) { if (y >= 0 && y < mapHeight && x >= 0 && x < mapWidth) { return map[y][x]; //Computer reads Y-axis first, X-Axis after } return 'X'; } }; class PlayerChar //Private from start, struct is used to clump variables togeather without worrying about changes { private: public: char symbol = 1; int POSx = 1; int POSy = 1; void Draw() { SetColor(Color::lightTEAL, Color::BLUE); //Use :: for clarification. Called Scope when using :: MoveCursor(POSy, POSx); cout << symbol; } void TryMoveTo(int y, int x, char ObjectAt) { switch (ObjectAt) { case 'X': break; case ' ': POSy = y; POSx = x; break; case 'T': POSy = 10; POSx = 10; break; case 'O': POSy = 5; POSx = 5; break; default: break; } } }; void ShowObject() { Map Draw; Draw.DrawObject(8, 8, 'Z'); } int main() { #pragma region Size_of_Map int width = 20; int height = 20; //menu(); /* cout << "How big map do you want? (full screen is Width: 79, Height 21)" << endl << "Width (Max 79 or it will default to 10): "; cin >> width; cout << "Height (Max 21 or it will default to 10): "; cin >> height; if (width > 79 || width < 0) { width = 79; } else if (height > 21 || height < 0) { height = 21; } else { cout << "Critical error. Shutting down"; } system("CLS"); */ #pragma endregion Size_of_Map Map world(width, height); PlayerChar hero; char ObjectAt = ' '; ShowObject(); world.Draw(hero.POSy, hero.POSx, ObjectAt); hero.Draw(); int toPosX = 0; int toPosY = 0; bool wantstomove = false; bool play = true; while (play) { char movement = _getch(); wantstomove = false; switch (movement) { case 'a': //left { toPosX = hero.POSx - 1; toPosY = hero.POSy; wantstomove = true; break; } case 'w': //up { toPosY = hero.POSy - 1; toPosX = hero.POSx; wantstomove = true; break; } case 'd': //right { toPosX = hero.POSx + 1; toPosY = hero.POSy; wantstomove = true; break; } case 's': //down { toPosY = hero.POSy + 1; toPosX = hero.POSx; wantstomove = true; break; } case 'q': //quit { play = false; break; } case 'r': //reset { hero.POSx = 1; hero.POSy = 1; break; } default: //Do nothing if anything not binded is not binded. { break; } } if (wantstomove == true) { ObjectAt = world.GetObjectAt(toPosY, toPosX); //Call Map class with world., use function GetObjectAt(Y-coord, X-Coord) hero.TryMoveTo(toPosY, toPosX, ObjectAt); //Call PlayerChar class with hero., use function TryMoveTo(Y-coord, X-Coord, Send what object is where we are going to move } ShowObject(); world.Draw(hero.POSy, hero.POSx, ObjectAt); //don't pass any arguments in normal use. pass hero.POSx, hero.POSy for coords. hero.Draw(); } return 0; }

Dold text

Problemet kommer när jag försöker lägga till objekt på kartan via en utomstående funktion.

Permalänk
Medlem

Vad får du för error?

Visa signatur
Permalänk
Medlem

@TorskOlle:

Felet är här:

Map Draw;

Här instansierar du ett object av typen Map, men eftersom du inte anger några argument så körs default konstruktorn. Restultatet blir att din map är tom. Du kunde istället ha gjort följande för att anropa rätt konstruktor.

Map Draw(20, 20);

I övrigt kan det vara bra att direkt lära dig regel #1 vid dynamisk minnesallogering. Varje new ska motsvaras av en delete, annars får du minnes läckage. Har du new i konstruktorn kan det vara rimligt att ha delete i destruktorn. I övrigt, kör hårt och lycka till med proggandet!

Visa signatur

Louqe Ghost S1 MK3 | Asus ROG Strix B660-I Gaming WiFi | Intel Core i7 12700K | nVidia RTX 2070 Super FE | Corsair 64GB (2x32GB) DDR5 5600MHz CL40 Vengeance | Samsung 980 PRO M.2 NVMe SSD 2TB | Corsair SF750 750W 80+ Platinum | Noctua NH-L12 Ghost S1 edition | Kablar från pslate customs | 2 stk Dell Ultrasharp 3014 | Logitech MX Keys | Logitech MX Anywhere

Permalänk

Du borde nog inte ens ha en default-konstruktor i det här fallet. Är det vettigt att köra meden tom karta?