I suspect it's a problem with pointers, but I'm fairly new to C++ and I'm having some trouble debugging this. I have a getX() method in my Vector class I'm writing, but it never prints the int I pass in.
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;
}
The constructor updates local variables instead of class members. Try
Vector2::Vector2(int u, int v) {
x = u;
y = v;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.