Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7188509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:06:02+00:00 2026-05-28T19:06:02+00:00

I’ve got weird problem. I’m trying to write simple game in C++, but I

  • 0

I’ve got weird problem. I’m trying to write simple game in C++, but I failed on objects and data types. There’s a code:

// C++
// Statki

#include <stdio.h>
#include <time.h>
#include <map>
#include <vector>
#include <string>
#include <list>

#define D true

using namespace std;

void _(char* message){printf("%s\n",message);};

struct relpoint { int x,y; };
struct point { int x,y; };
struct size { int w,h; };

map<const char*, vector<relpoint> > shipshape;
list<char*> shipTypes = {"XS", "S", "M", "L", "XL"};

string alpha="ABCDEFGHIJKLMNOPRSTUVWXYZ";

enum fieldtype { UNKNOWN=-1,EMPTY=0,SHIP=1,HIT=2,MISS=3,};

enum rotation { EAST=0, SOUTH=1, WEST=2, NORTH=3 };

class Ship
{
    char* type;

};

class Sea
{
    public:
    void init(size mapsize) { init( mapsize, EMPTY ); };
    void init(size mapsize, fieldtype fill)
    {
        if(D)printf("Generating sea\n");
        vector<fieldtype> v;

        seamap.reserve(mapsize.h);
        v.reserve(mapsize.w);

        for (int y=0; y<mapsize.h; y++)
        {
            v.clear();
            for(int x=0; x<mapsize.w; x++)
            {
                v.push_back(fill);
            }
            seamap.push_back(v);
        }

        view();
    };

    bool place_ship(Ship ship);

    void view()
    {
        for( vector< vector<fieldtype> >::const_iterator yy = seamap.begin(); yy != seamap.end(); ++yy )
        {
            for( vector<fieldtype>::const_iterator xx = (*yy).begin(); xx != (*yy).end(); ++xx )
            {
                if(D)printf("%d ", *xx);
            }
            if(D)printf("\n");
        }
    };

    private:
    vector< vector<fieldtype> > seamap;
};

class Game
{
    public:

    void initmap(size mapsize)
    {
        if(D) printf("\nInit %d×%d map\n", mapsize.w, mapsize.h);

        (*enemymap).init(mapsize, UNKNOWN);
        //(*selfmap).init(mapsize);
    };

    bool placeship(string type, point position, rotation rotate);
    fieldtype shoot(point target);
    void viewmap(){(*selfmap).view();};

    bool eog();

    Sea * enemymap;
    Sea * selfmap;
};

class Bot
{
    public:

    void init(size mapsize)
    {
        if(D)_("Init Bot");
    }

    private:

    Game * g;
};

class Player
{

    public:
    Player() { if(D){_("Player fake init");} };

    void init(size mapsize)
    {
        (*g).initmap(mapsize);
    };

    void viewmap(){(*g).viewmap();};

    private:
    Game * g;

};

class Router
{

    public:

    void startgame();
    void welcomescreen()
    {
        printf("\n\n\n\t\t\tShips minigame\n\t\t\t\tby Kris\n\n");

        mainmenu();
    };
    void mainmenu()
    {
        printf("Menu (type letter):\n\tN: New game\n\tS: Settings\n\tQ: Quit game\n\n > ");

        char opt;
        opt = toupper(getchar());

        size ms;

        switch(opt)
        {
            case 'N':
                ms = getmapsize();
                (*P1).init(ms);
                (*P2).init(ms);
            break;

            case 'S':

            break;

            case 'Q':

            break;

            default:
                printf("Invalid option %c", opt);
                mainmenu();
        }
    };

    private:
    Player * P1;
    Bot    * P2;

    size getmapsize()
    {
        size ms;
        printf("\nSet map size (X Y)\n > ");
        scanf("%d %d", &ms.w, &ms.h);
        return ms;
    };
};

int main () {

    vector<relpoint> shp;
    shp.reserve(5);
    list<char*>::const_iterator tp = shipTypes.begin();
    shp.push_back({0,0});
    shipshape[*(tp++)] = shp;
    shp.push_back({1,0});
    shipshape[*(tp++)] = shp;
    shp.push_back({2,0});
    shipshape[*(tp++)] = shp;
    shp.push_back({3,0});
    shipshape[*(tp++)] = shp;
    shp.push_back({2,1});
    shipshape[*tp] = shp;

    Router R;
    R.welcomescreen();

    printf("\n\n");
    return 0;
}

It can be compiled, but after line Init 5×5 map program stops with Naruszenie ochrony pamięci (Memory access violation in polish) error. Problem seems to occur at both Sea::init() functions.

I’m compiling it with g++ -std=c++0x -Wno-write-strings ships2.cpp (to prevent warnings) on Ubuntu.

Any idea what’s wrong with it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T19:06:03+00:00Added an answer on May 28, 2026 at 7:06 pm

    You are using an uninitialized pointer. You can fix it by instantiating an object here, or in a constructor somewhere else.

    Here is an example of instantiating in the initmap call.

    void initmap(size mapsize)
    {
        // Initialize the pointer by instantiating a class
        enemymap = new Sea;
        if(D) printf("\nInit %d×%d map\n", mapsize.w, mapsize.h);
    
        (*enemymap).init(mapsize, UNKNOWN);
        //(*selfmap).init(mapsize);
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Seemingly simple, but I cannot find anything relevant on the web. What is the
I want to construct a data frame in an Rcpp function, but when I
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.