数组对,乱码输出(未分配内存?) [英] Array of pairs, gibberish output (unallocated memory?)

查看:205
本文介绍了数组对,乱码输出(未分配内存?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自Java我试图在C ++中实现一个简单的战舰游戏,但已经困在这个数组:

  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 coords [i] = make_pair(x,y + i)
}
}
else {
for(int i = 0; i coords [i] = make_pair(x + y)。
}
}
}
void Ship :: printship(){
for(int i = 0; i cout < x:<< coords [i] .first<< ,y:< coords [i] .second<< endl;
}
}

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

我得到的是:

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

我想有东西指向未分配的内存,但我不知道什么,为什么。


< div有两个不同的变量,分别叫 coords 。一个是私有成员变量,另一个是构造函数的本地变量。因为您在构造函数中创建的局部变量会影响成员变量,所以构造函数不会初始化成员变量。



请尝试:

  #include< iostream> 
#include< utility>
#include< vector>

using namespace std;

class Ship {
private:
int length;
bool direction; // false = left,true = down
vector< pair< int,int> >协调// *** 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 coords.push_back(make_pair(x,y + i)); // *** CHANGE HERE
}
}
else {
for(int i = 0; i coords.push_back make_pair(x + i,y)); // *** CHANGE HERE
}
}
}
void Ship :: printship(){
for(int i = 0; i cout<< x:<< coords [i] .first<< ,y:< coords [i] .second<< endl;
}
}

int main(){
Ship tests(2,3,true,3);
tests.printship();
return 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.

解决方案

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;
}

这篇关于数组对,乱码输出(未分配内存?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆