簡體   English   中英

C ++ Getter方法打印怪異數字

[英]C++ Getter Method Printing Weird Numbers

我懷疑這是指針的問題,但是我對C ++還是很陌生,調試時遇到了一些麻煩。 method in my Vector class I'm writing, but it never prints the int I pass in. 我正在編寫的Vector類中有一個方法,但是它從不打印傳入的int。

main.cpp

#include <iostream>
#include "Vector2.h"
using namespace std;
int main() {
    Vector2 vec2 = Vector2(5, 6);
    cout << vec2.getX() << endl;
    return 0;
 }

Vector2.cpp

#include "Vector2.h"`

Vector2::Vector2(int u, int v) {
    // TODO Auto-generated constructor stub
    int x = u;
    int y = v;
}
int Vector2::getX() {
        return x;
}
int Vector2::getY() {
        return y;
}
Vector2::~Vector2() {
    // TODO Auto-generated destructor stub
}

Vector2.h

#ifndef VECTOR2_H_
#define VECTOR2_H_

class Vector2 {
    int x;
    int y;
public:
    Vector2(int x, int y);
    int getX();
    int getY();
    virtual ~Vector2();
 };

#endif /* VECTOR2_H_ */
Vector2::Vector2(int u, int v) {
    // TODO Auto-generated constructor stub
    int x = u;
    int y = v;
}

構造函數更新局部變量而不是類成員。 嘗試

Vector2::Vector2(int u, int v) {
    x = u;
    y = v;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM