I am trying to pass a pointer into my classes function, have it incremented, and have the variable retain it's value using pointers. Heres my code, it doesnt increment.
#include "stdafx.h"
#include <iostream>
using namespace std;
class test
{
public:
int addTo();
test(int * currentY);
private:
int y;
};
test::test(int * currentY):
y(*currentY)
{
}
int test::addTo()
{
y++;
return 0;
}
int main ()
{
for (;;)
{
int pointedAt = 1;
int * number = &pointedAt;
test t(number);
t.addTo();
cout <<*number;
char f;
cin >>f;
}
}
This should do it:
#include "stdafx.h"
#include <iostream>
using namespace std;
class test
{
public:
int addTo();
test(int * currentY);
private:
int *y;
};
test::test(int *currentY):
y(currentY)
{}
int test::addTo()
{
++*y;
return 0;
}
int main ()
{
for (;;)
{
int pointedAt = 1;
test t(&pointedAt);
t.addTo();
cout << pointedAt;
}
}
You have to store a pointer to the integer, so it refers to the same address as the original variable.
Related
What I want to do (c++ problem):
Enter main. Call Class A (and pass a value). Inside class A, I call Class B (and pass a value). Do some stuff in class B. Return value back to Class A. Do some more stuff in A. Return back to main function.
I get the error that obj3 is an unknown override specifier. I tried to create a simple program to showcase my problem;
#include <math.h>
#include <string>
#include <stdio.h>
#include <iomanip>
#include <iostream>
using namespace std;
class A
{
B obj3;
public:
int add3(int num)
{
int x = num + 1;
int y = obj3.add2(x);
return y;
}
};
class B
{
public:
int add2(int num2)
{
int y = num2 + 2;
return y;
}
};
int main()
{
int g;
A obj1;
cout << "enter a number: " << endl;
cin >> g;
int r = obj1.add3(g);
cout << r;
system("pause");
return 0;
}
How to assign/read a member of an object in a stack?
struct item{
char opra;
int count;
double operand;
};
stack<item> S;
double test = S.top.operand;
it not works, thanks.
Top is a method, so you should call S.top().operand. I got it to compile like that:
#include <iostream>
#include <stack>
struct item{
char opra;
int count;
double operand;
};
int main(){
std::stack<item> S;
double test = S.top().operand;
return 0;
}
You are just making a typo.
top in stack is a function, I let you here a working example:
#include <iostream>
#include <stack>
using namespace std;
struct item{
char opra;
int count;
double operand;
};
int main()
{
item a;
a.opra = 'a';
a.count = 3;
a.operand = 5.0;
stack<item> S;
S.push(a);
// Stack top
cout << S.top().operand;
return 0;
}
Output:
5
#include<iostream>
using namespace std;
class money
{
int rs;
int p;
public:
void setdata (int x , int y)
{rs=x; p=y;}
void show()
{ cout <<rs <<"." <<p; }
money operator += (int a) {
money temp;
temp.rs=rs+a.rs;
temp.p=p+a.p;
return (temp);
}
};
int main() {
money c1,c2;
c1.setdata(8,2);
c2=c1.operator+=(4);
c2.show();
}
Can someone tell me why the operator += overloading doesn't work?
My desiring output is 12.2 but the output i got is 16.2 .
I am sending 4 as argument and i want this argument is added in r (ruppee)
part
#include<iostream>
using namespace std;
class money
{
int rs;
int p;
public:
void setdata (int x , int y)
{rs=x; p=y;}
void show()
{ cout <<rs <<"." <<p; }
money& operator+=(int a)
{ rs += a; return *this; }
};
int main() {
money c1,c2;
c1.setdata(4,2);
c2=c1+=(4); //c2=c1.operator+=(4);
c2.show();
}
Try to use constructor correctly.
For example:
#include <iostream>
using namespace std;
class Example
{
public:
int x;
Example(int a)
{
x=a;
}
Example operator+(Example obj)
{
Example ans(0);
ans=x+obj.x;
return ans;
}
};
int main()
{
Example a(10),b(20);
Example ans=a+b;
cout<<ans.x<<endl;
return 0;
}
Hi I am working on a program that involves a Main.cpp, Connect4.cpp, and Connect4.h file. When I compile my program I am getting an error in the Main file saying that my playGame function is an undefined reference. I am compiling both files together(main first) I believe something is wrong in the way I am trying to dynamically call the function playGame. Any input would be much appreciated!
Main.cpp
#include <iostream>
#include <array>
#include "Connect4.h"
void playGame();
using namespace std;
int main()
{
Connect4 *ptr;
ptr=new Connect4;
ptr-> playGame();
delete ptr;
}
Connect4.cpp
#include <iostream>
#include <array>
#include "Connect4.h"
char gameBoard[9][7];
int rows;
int columns;
using namespace std;
void playGame()
{
void display();
int selectColumn(bool);
int tokenPlacement(char token, int columns);
bool winOrLose();
cout<<"Welcome to Connect Four.";
for(int i=0; i<rows;++i)
{
for(int j=0; j<columns; ++j)
{
gameBoard[i][j]=' ';
}
}
bool player1Turn=true;
char winner='n';
int column =0;
while(true){
display();
column=selectColumn(player1Turn);
if(player1Turn==true)
{
tokenPlacement('x',column);
player1Turn=false;
}
else
{
tokenPlacement('o', column);
player1Turn=true;
winner= winOrLose();
if(winner!='n')
{
break;
}
}
cout<<"Winner is:"<<winner;
}
Connect4.h
#ifndef CONNECT4_H_
#define CONNECT4_H_
#include
using namespace std;
class Connect4 {
public:
static void playGame();
private:
void display();
int selectColumn(bool);
int tokenPlacement(char, int);
bool winOrLose();
char gameBoard[9][7];
};
#endif /* CONNECT4_H_ */
I am trying to implement a biginteger class, and after I created a biginteger class, with a proper header file, and at first I am trying to define a operator=() operator, so when I make a new biginteger object, I will be able to make it equals with a integer.
This is the main.cpp:
#include <iostream>
#include "bigint.h"
using namespace std;
int main()
{
bigint bela = 15;
cout << "Hello world!" << bela.mennyi() <<endl;
return 0;
}
And this is the biginteger header:
#ifndef BIGINT_H
#define BIGINT_H
#include <vector>
#include <iostream>
class bigint
{
public:
bigint();
void operator=(const int &a);
int mennyi();
protected:
private:
std::vector<int> numarray;
};
#endif // BIGINT_H
And the biginteger.cpp file:
#include "bigint.h"
#include <iostream>
using namespace std;
bigint::bigint()
{
numarray.resize(0);
}
void bigint::operator=(const int &a)
{
int b = a;
if(b >= 0)
{
numarray.resize(0);
while(b!=0){
numarray.push_back(b%10);
b = b/10;
}
}
}
int bigint::mennyi()
{
int ki = 0;
for(int i = (numarray.size())-1; i>=0; i--)
{
ki = ki*10 + numarray[i];
}
return ki;
}
When I start the debugging I get an error saying: conversion from 'int' to non-scalar type 'bigint' requested.
You should implement this constructor:
bigint::bigint(int);