I'm trying to pass a reference of a class through a void function, but it throws an error.
Here is the code (it has to be a void function and not return anything). If I change the function to return int or string it works fine but I don't want to do that.
#include <iostream>
using namespace std;
class car
{
public:
car()
: wheels(4)
{
}
int wheels;
};
void getwheels( car& i_car )
{
//do something here
}
int main()
{
car mycar;
mycar.wheels = 6;
cout << getwheels( mycar )<< endl;
}
The void is the problem.
getwheels returns void, but you're printing it out as if it has a return value. If a function returns nothing, you can't print the result of calling it.
To solve, just call the function without printing:
getwheels( my_car );
Or if what you meant to do was print out the wheels value, print the value inside the function:
void getwheels(car& i_car)
{
cout << i_car.wheels << endl;
}
Try to return wheels from getwheels instead of void
int getwheels(const car& i_car)
{
return i_car.wheels;
}
Or pass std::ostream into getwheels:
std::ostream& getwheels(std::ostream& out, const car& i_car)
{
//do something here
out << i_car.wheels << std::endl;;
return out;
}
int main()
{
car mycar;
mycar.wheels = 6;
getwheels(std::cout, mycar);
}
Related
Say we have 2 functions
foo() { cout << "Hello"; }
foo2() { cout << " wolrd!"; }
how can i create an array of pointers (say a, b), with a pointing to foo() and b to foo2() ?
my goal is to store these pointers in an array A, then loop over A to execute these functions.
You can use typed function pointers as follows:
using FunPtrType = void(*)();
FunPtrType arr[]{&foo, &foo2};
// or
std::array<FunPtrType, 2> arr2{&foo, &foo2};
// ... do something with the array of free function pointers
// example
for(auto fun: arr2)
fun();
There is a simple implementation:
#include <iostream>
#include <vector>
using namespace std;
// Defining test functions
void a(){cout<<"Function A"<<endl;}
void b(){cout<<"Function B"<<endl;}
int main()
{
/*Declaring a vector of functions
Which return void and takes no arguments.
*/
vector<void(*)()> fonc;
//Adding my functions in my vector
fonc.push_back(a);
fonc.push_back(b);
//Calling with a loop.
for(int i=0; i<2; i++){
fonc[i]();
}
return 0;
}
There's no need for typedefs these days, just use auto.
#include <iostream>
void foo1() { std::cout << "Hello"; }
void foo2() { std::cout << " world!"; }
auto foos = { &foo1, &foo2 };
int main() { for (auto foo : foos) foo(); }
There are two equivalent ways to do what you want:
Method 1
#include <iostream>
void foo()
{
std::cout << "Hello";
}
void foo2()
{
std::cout << " wolrd!";
}
int main()
{
void (*a)() = foo;// a is a pointer to a function that takes no parameter and also does not return anything
void (*b)() = foo2;// b is a pointer to a function that takes no parameter and also does not return anything
//create array(of size 2) that can hold pointers to functions that does not return anything and also does not take any parameter
void (*arr[2])() = { a, b};
arr[0](); // calls foo
arr[1](); //calls foo1
return 0;
}
Method 1 can be executed here.
Method 2
#include <iostream>
void foo()
{
std::cout << "Hello";
}
void foo2()
{
std::cout << " wolrd!";
}
int main()
{
//create array(of size 2) that can hold pointers to functions that does not return anything
void (*arr[2])() = { foo, foo2};
arr[0](); // calls foo
arr[1](); //calls foo1
return 0;
}
Method 2 can be executed here.
How to read the following code for main?
I do not know this
Code :
class one
{
public:
void operator()() const
{
f();
f1();
}
};
I want to call the operator To main?
void operator()() const defines a function call operator, which can be used as:
one ob;
ob(); // calls ob.operator()()
For another, more complete, example.
#include <iostream>
class Two
{
public:
int operator()(const char *str) const
{
std::cout << "operator() called with " << str << std::endl;
return 101;
}
};
int main()
{
Two two;
int n = two("'test'");
std::cout << "operator() returned " << n << std::endl;
}
Output:
operator() called with 'test'
operator() returned 101
You can create an instance of the class in the main function and call the function using that instance.
class one
{
public:
void operator()() const
{
f();
f1();
}
};
int main() {
one obj_one;
// calling the member function -> method
obj_one.operator()();
return 0;
}
i just wanted to know if someone could help me about my code,i am a little confused to why it is not working like i want it to maybe i am misunderstanding something...The point of the program is to write a class with two functions to set and get a number but later on the main part of the code i have wanted it to print out a 2.52 number not just the number 2.Thank you if anyone helps :) .
#include <iostream>
#include<conio.h>
using namespace std;
class Class
{
public:
void Set(float x)
{
number = x;
}
int Get()
{
return number;
}
private:
float number;
};
int main()
{
Class object;
object.Set(2.52);
cout << "The number is: " << object.Get();
return 0;
}
First of all, you return an int from Get() so number will be converted in an int.
You should also make Get() const since it's will not change anything in the Class object when you call the function. Making it const makes it possible to pass instances of Class to functions taking a Class by const&:
#include <iostream>
class Class
{
public:
void Set(float x)
{
number = x;
}
float Get() const // returning float and added const
{
return number;
}
private:
float number;
};
void tester(const Class& obj) // a function taking a Class by const reference:
{
std::cout << "The number is: " << obj.Get() << '\n';
}
int main()
{
Class object;
object.Set(2.52);
tester(object);
}
Without the added const compilation would fail.
you can change get method type(float) like that
class Class
{
public:
void Set(float x)
{
number = x;
}
float Get()
{
return number;
}
private:
float number;
};
int main()
{
Class object;
object.Set(2.52);
cout << "The number is: " << object.Get();
return 0;
}
Working on a small role-playing game battle system to practice object-oriented code. I have a Party class, which has a vector to store a variable amount of party members.
Initializing my party and adding a member seems to work great. I'm even able to call the member's take_damage() function to change the member's hp and that seems to work too.
But when I check on the same member's hp on the next line using the hp getter, it's right back where it started.
I made a destructor for the member class to see what was going on and according to the output the object is being destroyed many times. Why is this?
class Member
{
private:
int hp;
public:
Member() { hp = 1; }
~Member() { std::cout << "DESTROYED!!" << std::endl; }
int get_hp() { return hp; }
void take_damage(int amt) { hp += amt }
};
class Party {
private:
std::vector<Member> members;
public:
void add_member(Member memb) { members.push_back(memb); }
Member get_member(int num) { return members[num]; }
};
int main() {
Party p;
Member m;
p.add_member(m);
std::cout << p.get_member(0).get_hp() << std::endl;
p.get_member(0).take_damage(4);
std::cout << p.get_member(0).get_hp() << std::endl;
}
Your get_member method returns a copy of the array element, rather than a reference to it. Return a reference, and the member will be modified.
Member& get_member(int num) { return members[num]; }
see small issue in code -=amt
also changed function of get_member
other than that, the code seems fine.
class Member
{
private:
int hp;
public:
Member() { hp = 1; }
~Member() { std::cout << "DESTROYED!!" << std::endl; }
int get_hp() { return hp; }
void take_damage(int amt) { hp -= amt } // This should be -=amt, not +=amt
};
class Party {
private:
std::vector<Member> members;
public:
void add_member(Member memb) { members.push_back(memb); }
Member& get_member(int num) { return members[num]; }
};
int main() {
Party p;
Member m;
p.add_member(m);
std::cout << p.get_member(0).get_hp() << std::endl;
p.get_member(0).take_damage(4);
std::cout << p.get_member(0).get_hp() << std::endl;
}
I'm trying to pass a reference to a function in a class but am having trouble figuring out how to do it. So say I have a class test defined as such
#include <iostream>
class test {
public:
test () {};
~test () {};
void setA (int);
int getA (void);
private:
int a;
};
void test::setA (int A) { a = A; }
int test::getA (void) { return a; }
using namespace std;
int main ()
{
test T;
T.setA(5);
cout << "a = " << T.getA() << endl;
return 0;
}
That works fine but if I want to pass the values by reference
#include <iostream>
class test {
public:
test () {};
~test () {};
void setA (int);
int & getA (void);
private:
int a;
};
void test::setA (int & A) { a = A; }
int & test::getA (void) { return a; }
using namespace std;
int main ()
{
test T;
T.setA(5);
cout << "a = " << T.getA() << endl;
return 0;
}
I cannot figure out how to configure setA to pass by reference.
There are two issues with the code. First, the definition of setA does not match the declaration. You must make the declaration take in a reference as a parameter.
Change this:
void setA (int);
To this:
void setA (int&);
The second issue is that you are trying to pass an r-value (5) as a reference. You must pass in an l-value. You can do that by creating an int first and then passing that by reference:
int i = 5;
T.setA(i);
Full example:
#include <iostream>
class test {
public:
test () {};
~test () {};
void setA (int&);
int & getA (void);
private:
int a;
};
void test::setA (int & A) { a = A; }
int & test::getA (void) { return a; }
using namespace std;
int main ()
{
test T;
int i = 5;
T.setA(i);
cout << "a = " << T.getA() << endl;
return 0;
}
When you pass something by reference to a function in C++, the function does not keep the parameter in memory automatically. Thus, you have to declare it before so that it stays in memory throughout the entire function.
The 5 you tried to pass as a reference would go out of scope and get destroyed as soon as the function starts. The declared i variable is instead destroyed at the end of the main function.
The reason is because in order to pass by reference, you must have an lvalue, which is a fancy way of saying something that persists beyond a single use.
If you created an int variable, you would be able to pass it in by reference. In the code above, you attempted to pass in a raw integer value (5), which fails, since the compiler is expecting a reference to an int, not a raw integer value.
The following code would work:
int main ()
{
test T;
int myVariable = 4; // Need an actual variable to pass by reference.
T.setA(myVariable);
cout << "a = " << T.getA() << endl;
return 0;
}
However, if you want your function to take raw integer values like you showed in your second example, you must have a function definition like your first example, where all the function takes is an integer. Hope this helps!
Maybe you could try this:
#include <iostream>
class test {
public:
test() {};
~test() {};
void setA(int&&); // requires at least C++11
void setA(int&);
int & getA(void);
private:
int a;
};
void test::setA(int && A) { a = A; }
void test::setA(int&A) { a = A; }
int & test::getA(void) { return a; }
using namespace std;
int main()
{
test T;
int i = 5;
T.setA(i);
cout << "a = " << T.getA() << endl;
T.setA(8);
cout << "a = " << T.getA() << endl;
return 0;
}
In the example, int& passes a l-value while int&& passes a r-value as a reference.