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 8519699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:19:20+00:00 2026-06-11T06:19:20+00:00

Coming from Java I was trying to implement a simple Battleships game in C++

  • 0

Coming from Java I was trying to implement a simple Battleships game in C++ but already got stuck at this Array:

#include <iostream>
#include <utility>

using namespace std;

class Ship{
    private:
        int length;
        bool direction; //false = left, true = down
        pair <int,int> coords[];
    public:
        Ship(int x, int y, bool, int);
        void printship();
};

Ship::Ship(int x, int y, bool dir, int l){ 
    pair <int,int> coords[l];
    length = l;
    if (dir){
        for (int i = 0; i < l; i++){
            coords[i] = make_pair(x, y+i);
        }   
    }   
    else{
        for (int i = 0; i < l; i++){
            coords[i] = make_pair(x+i, y); 
        }   
    }   
}
void Ship::printship(){
    for (int i = 0; i < length; i++){
        cout << "x: " << coords[i].first << ", y: " << coords[i].second << endl;
    }   
}

int main(){
    Ship tests(2,3,true,3);
    tests.printship();
    return 0;
}

What I get is:

x: 134515168, y: 0
x: 0, y: 9938131
x: 1, y: -1080624940

I guess something is pointing to unallocated memory, but I can’t figure out what, and why.

  • 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-06-11T06:19:21+00:00Added an answer on June 11, 2026 at 6:19 am

    You have two different variables both called coords. One is a private member variable, the other is local to the constructor. Because the local variable you create in the constructor shadows the member variable, the constructor never initializes the member variable.

    Try this instead:

    #include <iostream>
    #include <utility>
    #include <vector>
    
    using namespace std;
    
    class Ship{
        private:
            int length;
            bool direction; //false = left, true = down
            vector< pair <int,int> > coords; // *** CHANGE HERE
        public:
            Ship(int x, int y, bool, int);
            void printship();
    };
    
    Ship::Ship(int x, int y, bool dir, int l){ 
        length = l;
        if (dir){
            for (int i = 0; i < l; i++){
                coords.push_back(make_pair(x, y+i)); // *** CHANGE HERE
            }   
        }   
        else{
            for (int i = 0; i < l; i++){
                coords.push_back(make_pair(x+i, y)); // *** CHANGE HERE
            }   
        }   
    }
    void Ship::printship(){
        for (int i = 0; i < length; i++){
            cout << "x: " << coords[i].first << ", y: " << coords[i].second << endl;
        }   
    }
    
    int main(){
        Ship tests(2,3,true,3);
        tests.printship();
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm coming from a Java background and trying to port a simple version of
I am trying to implement a Queue in C. Coming from Java and other
I'm trying to learn Erlang, coming from a C++/Java background. This forces me to
I am coming from Java to Ruby and this -7 mod 3 = 2
Coming from a Java background, this is the way I'm thinking: The server provides
I am new to Java coming from a PHP background so sorry if this
I'm trying to structure my app in Python. Coming back from C#/Java background, I
I just started learning C++(coming from Java & Python) and am trying to learn
I'm coming from a Java background, with its class-based inheritance model, trying to get
Coming from Java Land I have been trying to teach myself Scala. Recently I

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.