possible scope issue c++ - c++

#include<iostream>
#include<vector>
using namespace std;
class t{
public:
t();
void updateSize();
int getSize();
void insert();
int get(int a);
private:
int size;
vector<int> v;
};
t::t(){
size =0;
}
void t::updateSize(){
size++;
}
int t::getSize(){
return size;
}
int t::get(int a){
return v[a];
}
void t::insert(){
v.push_back(size);
++size;
}
int main(){
t xa;
xa.insert();
xa.insert();
xa.insert();
xa.insert();
cout<<xa.get(3);//expect to output 3 but instead outputs 0
return 0;
}
this code is supposed to increment the size every time I call insert, and put an integer of with the value of that size in a vector at the same index of that size. But for some reason it does not put the updated size into my vector.

You're inserting 3 elements but you're reading the 4th (since the indexing is 0 based).

The program you posted will print "3". Proof read your code.

Related

Why is my method with array parameter not working properly? (C++)

I'm new with c++ and I've been meaning to implement vector calculations via operator overloading.
The code that's not functioning as I intended is this.
First, main.cpp
#include <iostream>
#include "MyVector.h"
#include "MyVector.cpp"
int main() {
double forvector1[] = {0.1,0.2,0.3};
double forvector2[] = {0.2,0.3,0.5};
MyVector vector1(forvector1,3);
MyVector vector2(forvector2,3);
MyVector temp = vector1 + vector2;
temp.print();
return 0;
}
Then, MyVector.cpp
#include "MyVector.h"
#include <iostream>
using namespace std;
MyVector::MyVector(double numList[], int size) : numList(numList), size(size) {
}
MyVector::MyVector(){ //empty vector;
}
void MyVector::print(){
cout<<"("<<numList[0];
for(int i=1;i<size;i++){
cout<<", "<<numList[i];
}
cout<<")"<<endl;
}
MyVector MyVector:: operator+(MyVector vec){
if(vec.size != size){
cout<<"+ cannot be applied to ";
cout<<"("<<numList[0];
for(int i=1;i<size;i++){
cout<<", "<<numList[i];
}
cout<<") and ";
vec.print();
return MyVector();
}
double tempList[size];
for(int i=0;i<size;i++)
{
tempList[i]=numList[i]+vec.numList[i];
}
MyVector result(tempList,size);
return result;
}
Finally, this is my MyVector.h
class MyVector{
private:
int size;
double * numList;
public:
MyVector(double numList[], int size); //size>=1
MyVector(); //empty Vector
void print(); //print its content e.g. (1, 2, 3, 4)
MyVector operator-(MyVector vec);
MyVector operator+(MyVector vec);
double operator*(MyVector vec);
MyVector operator/(MyVector vec);
//You may add more member functions as well
};
#endif // MYVECTOR_H_INCLUDED
According to the main.cpp, I should be getting (0.3, 0.5, 0.8) for the output. However, I keep getting (0.3, 0, 2.12199e-314), which means probably only the first element of the result array was right. I'm guessing it's because of the pointer I used to point at the array, so that's why only the first element was correct. Is there any way I could make the operator+ work? Any help would be appreciated. Thanks!
My guess would be that there is a dangling pointer.
double tempList[size];
for(int i=0;i<size;i++)
{
tempList[i]=numList[i]+vec.numList[i];
}
MyVector result(tempList,size);
return result;
tempList points to data that are local to the function, but result uses it. In your constructor, you should copy the data in the array into another array owned by the object.

How to swap array using pointer when array is a data member of a class

I am trying to swap the content in the arrays by swapping the pointers pointing to the two arrays.
My method is the same as what Daniel answered in this question: Swap arrays by using pointers in C++. But the difference is that my array will be a member in a class.
My code can be compiled successfully, but the output results are quite weird.
This is my header file:
#include <stdio.h>
#include <iostream>
class Map
{
public:
Map(int times); // Create an empty map (i.e., one with no key/value pairs)
int size(); // Return the number of key/value pairs in the map.
void dump();
void swap(Map &other);
int *retrieve();
void setptr(int *newptr);
private:
int *ptr;
int array_1[5];
};
Here is my implementation:
#include "Map.h"
#include <iostream>
using namespace std;
Map::Map(int times) {
for (int i = 0; i < 5; i++) {
array_1[i]=i*times;
}
ptr=array_1;
}
void Map::dump() {
ptr=array_1;
for (int i = 0; i < 5; i++) {
cout << *ptr << endl;
ptr++;
}
for (int i = 0; i < 5; i++) {
ptr--;
}
}
void Map::swap(Map &other) {
int *temp;
temp = this->ptr;
this->ptr = other.retrieve();
other.setptr(temp);
}
int *Map::retrieve() {
return ptr;
}
void Map::setptr(int *newptr) {
ptr=newptr;
}
Can anyone tell me what is wrong and how to implement it smartly?
The following code runs fine:
#include <stdio.h>
#include <iostream>
#include <conio.h>
using namespace std;
class Map
{
public:
Map(int times); // Create an empty map (i.e., one with no key/value pairs)
int size(); // Return the number of key/value pairs in the map.
void dump();
void swap(int &other);
int *retrieve();
void setptr(int *newptr);
private:
int *ptr;
int array_1[5];
};
Map::Map(int times){
for (int i=0;i<5;i++){
array_1[i]=i*times;
}
ptr=array_1;
}
void Map::dump(){
for (int i=0;i<5;i++)
{
cout<<ptr[i]<<endl;
}
}
void Map::swap(int &other){
int *temp;
temp=this->ptr;
this->ptr=&other;
other = *temp;
}
int *Map::retrieve(){
return ptr;
}
void Map::setptr(int *newptr){
ptr=newptr;
}
int main()
{
Map m(2);
Map n(3);
m.dump();
m.swap(*n.retrieve());
m.dump();
getchar();
}
1) Added a main function
2) Changed Swap function
But the problem that christopher pointed out will still persist i.e the pointer will point to an array in another object.
Edit: You probably need something like this:
void Map::swap(Map &other){
Map *temp;
temp=this;
*this = other;
other = *temp;
}
Map *Map::retrieve(){
return this;
}
Note: it is probably not elegant.
The problem with your design is that the pointer refers to an array in the same object.
Suppose you have to objects a and b. If you swap their pointers, a.ptr will point to b.array_1 which contains the data. reciprocally b.ptr will point to a.array1.
Unfortunately if one of the object -- say b -- gets destroyed (because it was a local object that goes out of scope, or for whatever reason) the pointer of the remaining object would point to an array which doesn't exist anymore. This is UB.
To solve your issue, you'd neet to allocate an array dynamically in the constructor. Get rid of array_1 completely:
Map::Map(int times){
ptr=new int[5]; // or better define a constant to avoid hard coded sizes
for (int i=0;i<5;i++){
ptr[i]=i*times;
}
}
Note that if you use pointers, you need to ensure the invarients on it. This means that you should define also the copy constructor and the assignment operator (to avoid the ptr to be blindly copied), as well as a destructor (to delete the dynamically allocated array).
P.S.: I suppose that you are learning C++ and are not yet familiar with vectors. These would avoid all the hassles here
Edit: if you experience your problem before any object is destroyed, it's because of a bad implementation of dump(): you increment the pointer there in, so that it will no longer point to the start of the array.
void Map::dump(){
for (int i=0;i<5;i++){
cout<<ptr[i]<<endl; // don't change ptr value !!
}
}
One simple trick to avoid such problems, is to systematically declare the member functions that are not supposed to change the state of the object as const:
class Map {
...
void dump() const;
...
}
Then the compiler issues an error if you try to accidentally change a member.

passing 1d and 2d arrays by reference in c++

I am new to c++ and have a couple questions regarding passing arrays by reference to functions (so that the arrays are modified by the function). I realize there are similar questions that have been asked already, but there are a few points that I think were not covered in those previous questions (at least from what I saw). From what I have gathered so far, one can pass an array by reference by doing the following:
#include<iostream>
using namespace std;
void modify_array(int* a);
int main()
{
int array[10];
modify_array(&array[0]);
for(int i=0;i<10;i++)
{
cout<<array[i]<<endl;
}
}
void modify_array(int* a)
{
int i;
for(i=0;i<10;i++)
{
*(a+i)=i;
}
}
This makes sense to me but if I change the function to:
void modify_array(int* a)
{
int i;
for(i=0;i<10;i++)
{
a[i]=i; //line changed
}
}
This also works. Is there a difference? Or is the second just a short cut? Also in the case of passing 2d arrays I would have guessed that the following code would work:
#include<iostream>
using namespace std;
void modify_array(int* a);
int main()
{
int array[10][10];
modify_array(&array[0][0]);
}
void modify_array(int* a)
{
int i,j;
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
a[i][j]=i*j;
}
}
}
But this doesn't. From what I have seen in other related questions, you would do something like:
void modify_array(int (*a)[10])
{
int i,j;
//a[i][j]= blah blah blah;
}
or,
void modify_array(int (&a)[10][10])
{
int i,j;
//a[i][j]= blah blah blah;
}
What is the difference between these latter two function definitions? What do experienced c++ programmers recommend using: the (*a)[10][10] notation or the (&a)[10][10] notation?
Writing *(a+i)=i; or a[i]=i; are equivalent. The first is seen as an offset applied to the pointer to the array and assigning the value to the pointee, while the second is assigning the value to the element of the array.
However, when passing a pointer to a function modify_array(int* a), it cannot deduce that the pointee is a 2D array, and does not know what size to offset to address the other lines of the array, with a[i][j]=i*j;. For the compiler, it can only access the first dimension of the array.
The proper way to do what you need is this
#include<iostream>
using namespace std;
void modify_array(int (&a)[10][10]);
int main()
{
int array[10][10];
modify_array(array);
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
cout<<array[i][j]<<endl;
}
}
}
void modify_array(int (&a)[10][10])
{
int i;
for(i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
a[i][j]=i*j;
}
}
}
Live example
The function is expecting an int array of 10x10, passed by reference.

Initialize empty array in class

I've just tried:
class Test
{
public:
int iArray[][];
}
...is this not possible? Do I have to set a constant value?
like:
class Test
{
public:
const int iArray[5][4];
}
I want to define [x][y] later, just have the placements there. Else it wouldn't be "dynamic" and I don't want to use a vector because I want to be able to access the values by "X" and "Y".
I think better way to achieve this is to use pointers. You can do like this.
#include <cstdlib>
#include <iostream>
using namespace std;
class PointerTest {
private:
int** array;
int x, y;
public :
void setValue(int row, int col,int value);
int getValue(int row, int col);
PointerTest(int row, int col);
~PointerTest() {
for(int i=0;i<x;i++) {
delete array[y];
}
}
};
PointerTest::PointerTest(int row, int col) {
x=row, y=col;
for(int i=0;i<row;i++) {
*array=new int[col];
}
}
void PointerTest::setValue(int row, int col, int value) {
*(array[row])=value;
}
int PointerTest::getValue(int row, int col) {
return *(array[row]);
}
int main(int argc, char *argv[])
{
PointerTest* t=new PointerTest(4,5);
t->setValue(0,0,464);
cout<<"The value in array: "<<t->getValue(0,0)<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
What about
tempalte <int N1, int N2> class Test
{
public:
int iArray[N1][N2];
};
?
What about putting a std::vector in a vector?
std::vector< std::vector< const int > > iArray;
There aren't many reason to use "plain" arrays in C++.
If you want to decide int iArray[][]; size later then you can use vector< vector<int> > iArray;.
The other way is to use nested new[], which would be little complex.
No this is not possible. But you can have a pointer in your class like
int **ptr;
and then in the constructor or where ever allocate the memory for your array with
ptr = (int **)malloc( the size you want );
or with the "new[]"-operator in C++.
but if you are using C++ .. the best way is to use:
std::vector< std::vector< int >> array;
class Test
{
public:
Test()
{
iArray = new int*[5];
for(int i = 0; i < 5; i++)
iArray[i] = new int[4];
}
~Test()
{
for(int i = 0; i < 5; i++)
delete[] iArray[i];
delete[] iArray;
}
int** iArray;
};
Will allow you to allocate a 2d int array at runtime (in this example it is a 5x4), but in all honestly I would use vectors as pointed out by some other posters, you don't need to worry about freeing the memory afterwards like you do with the use of new.

C++ copy vector of objects to another

I want to copy to vectors with the same size and same type one to another but after printing the value it seems like it doesn't work correctly or it doesn't want to copy all pointers to data in each object. Thanks for help
Here is code:
std::vector<Vehicle> vehicles(2);
std::vector<Vehicle> vehiclesCopy(2);
vehicles[0].setX_pos(3);
for(int i=0;i<vehicles.size();i++)
vehiclesCopy.push_back(vehicles[i]);
cout<<vehicles[0].getX_pos()<<endl;
cout<<vehiclesCopy[0].getX_pos()<<endl;
Output:
3
0
Here is the Vehicle code
class Vehicle
{
private:
unsigned int x_pos,y_pos,velocity;
char type;
public:
void vehicle(char inType,
unsigned int inX_pos,
unsigned int inY_pos,
unsigned int inVelocity)
{
type=inType;
x_pos=inX_pos;
y_pos=inY_pos;
velocity=inVelocity;
}
unsigned int getMaxPassengers(){
return maxPassengers;
}
unsigned int getX_pos(){
return x_pos;
}
unsigned int getY_pos(){
return y_pos;
}
unsigned int getVelocity(){
return velocity;
}
char getType(){
return type;
}
void setX_pos(unsigned int input){
x_pos=input;
}
void setY_pos(unsigned int input){
y_pos=input;
}
void setVelocity(unsigned int input){
velocity=input;
}
void setType(char input){
type=input;
}
};
You create two vectors with size 2. Then you push all elements from one vector to the other. You now have one unmodified vector and the other vector with 4 elements. Pushing two elements at the end wont have any effect on the first element (the one you print).
To copy vectors use simple assignment:
vehiclesCopy = vehicles;
Or if you want to use a loop (why would you?), assuming they both have correct size (they do in your example):
for(int i=0;i<vehicles.size();i++) {
vehiclesCopy[i] = vehicles[i];
}
PS: this answer isnt the whole truth. If vehiclesCopy is really just a copy of vehicles you should not first construct an empty vector and then copy it, but instead use the right constructor. See here for details (overload (6) is your friend here).