In this program I got the error
[Error] no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream}' and 'numcall')
I can't understand how to get rid of it!!
#include<iostream>
using namespace::std;
class numcall
{
int a,b;
public:
numcall(int c,int d)
{
c = a;
c = b;
cout<<"Your First num is " << c << endl << "Your Second num is "<< d << endl;
cout << "You are in PARAMETER CONSTRUCTOR";
}
numcall(int u)
{
u = a;
b = 0;
cout << "Your First num is " << u << endl << "Your Second num is " << b << endl;
cout << "You are in PARAMETER CONSTRUCTOR";
}
numcall()
{
}
};
int main(void)
{
numcall x = numcall();
numcall y = numcall(3,4);
numcall z = numcall(3);
cout << x << endl << endl << y << endl << endl << z << endl;
}
You haven't defined the << operator for your class numcall, so the compiler doesn't know how to apply it.
So define it.
You need to define a friend method for the << stream operator, otherwise he doesn't know what to print.
friend ostream &operator<<(ostream &os, const numcall &numcall1) {
os << "a: " << numcall1.a << " b: " << numcall1.b;
return os;
}
This is just an example of implementation.
By the way there are other mistakes: c=a means assign a to c, you want to do the other way around. Welcome to the programming world mate ;)
Related
I'm starting learning to code in C++ (coming from VB.net) and i need some help in enum class usage.
I've done this simple code from a lerning exercise (originally divided in header.h and Exercise.cpp, but i putted it all together here):
#include <iostream>
#include <string>
#define BEGIN_WITH(x) { \
auto &_ = x;
#define END_WITH() }
using namespace std;
enum class Gender { Male, Female };
struct PersonStruct {
string _Name;
string _SurName;
int _Age;
double _Heigth;
Gender _Gender; };
class Person { public:
string _Name{};
string _SurName{};
int _Age{};
double _Heigth{};
Gender _Gender{}; };
int ModifyPerson(Person& PassPersona, PersonStruct Attribute) {
PassPersona._Name = Attribute._Name;
PassPersona._SurName = Attribute._SurName;
PassPersona._Heigth = Attribute._Heigth;
PassPersona._Age = Attribute._Age;
PassPersona._Gender = Attribute._Gender;
return(0); }
int main() { Person TestingPerson;
BEGIN_WITH(TestingPerson)
_._Age = 23;
_._Gender = Gender::Male;
_._Heigth = 1.94;
_._Name = "John";
_._SurName = "Smith";
END_WITH()
cout << "Person attributes: " << endl;
cout << "Name: " << TestingPerson._Name << endl;
cout << "Surname: " << TestingPerson._SurName << endl;
cout << "Age: " << TestingPerson._Age << endl;
cout << "Gender: " << TestingPerson._Gender << endl;
cout << "Heigth: " << TestingPerson._Heigth << endl;
cout << endl;
ModifyPerson(TestingPerson, PersonStruct{ "Poca","Hontas",24,1.85,Gender::Female });
cout << "New Person attributes: " << endl;
cout << "Name: " << TestingPerson._Name << endl;
cout << "Surname: " << TestingPerson._SurName << endl;
cout << "Age: " << TestingPerson._Age << endl;
cout << "Gender: " << TestingPerson._Gender << endl;
cout << "Heigth: " << TestingPerson._Heigth << endl;
return(0); }
I've made the structure in order to group all Person class parameters. Doing this i've learned that using enum class is more secure than class. But when i switched to enum class a lot of errors prompted on my code. I've solved almost all of them, except this:
cout << "Gender: " << TestingPerson._Gender << endl;
Error code E0349 "no operator "<<" matches these operands"
I've searched around but i find no solution to this. Thanks in advance for your time! (any suggestion or recommendation on my code should be appreciated)
<< operator is not defined for class Gender because it's your own class. You'll need to overload it. More info here.
enum class Gender
{
male,
female
};
ostream& operator<< (ostream & os, const Gender & g)
{
switch(g)
{
case Gender::male: os << "Male"; return os;
case Gender::female: os << "Female"; return os;
}
return os;
};
I've recently started learning c++ and for the life of me, I can't seem to get the syntax of using ostream in a class and what arguments should I pass. Here's the code:
This is the class in question:
#include <iostream>
#include <string>
using namespace std;
class Pokemon{
friend ostream& operator<<(ostream&, Pokemon);
public:
string name, level, cp;
Pokemon(string x="Pikachu", string y="5", string z="1000"){
name = x;
level = y;
cp = z;
}
Pokemon name(){
return this->name;
}
Pokemon level(){
return this->level;
}
Pokemon cp(){
return this->cp;
}
Pokemon display_stats(){
cout << this-> name << "stats are:" << endl;
cout << " " << "Attack: 2716.05" << endl;
cout << " " << "Defence: 1629.63" << endl;
cout << " " << "HP: 1086.42" << endl;
}
};
template<typename TYPE> //i dont understand this and the things i've written down here are only based on samples i've seen
ostream& operator<<(ostream& os, Pokemon & c){
os << "The level of " << c.name << " is" << c.level << " with cp of " << c.cp;
}
As you could see, I already tried constructing the ostream thing but I don't really understand how it works. This is my main function:
int main()
{
Pokemon a, b, c, d;
a = Pokemon();
b = Pokemon("Weezing");
c = Pokemon("Nidoking", 100);
d = Pokemon("Mewtwo", 50, 5432.1);
cout << a << endl;
cout << b << endl;
cout << c << endl;
cout << d << endl;
cout << "Jessie: You are no match to me! Go " << b.name << "!" << endl;
cout << "Gary: Go lvl " << c.level << " " << c.name << "! Crush them" << endl;
cout << "Ash: " << a.name << " can do it even thouh he is only level " << a.level << endl;
cout << "Jessie: Hahaha! My " << b.name << " CP is " << b.cp << endl;
cout << "Gary: "<< c.name << " CP is " << c.cp << endl;
cout << "Ash: " << a.name << " CP is " << a.cp << endl;
cout << "Giovanni: Behold " << d.name << " is here." << endl;
d.display_stats();
return 0;
}
I'm getting errors of:
no instance of constructor "Pokemon::Pokemon" matches the argument list -- argument types are: (const char [9], int) //on line c = Pokemon("Nidoking", 100);
no instance of constructor "Pokemon::Pokemon" matches the argument list -- argument types are: (const char [7], int, double) //on line d = Pokemon("Mewtwo", 50, 5432.1);
All of your Pokemon class methods are returning the wrong type. And your main() is not calling any of the methods correctly at all.
Change your Pokemon class to look more like this:
#include <iostream>
#include <string>
using namespace std;
class Pokemon {
private:
string m_name;
int m_level;
double m_cp;
friend ostream& operator<<(ostream&, const Pokemon&);
public:
Pokemon(string x="Pikachu", int y=5, double z=1000) {
m_name = x;
m_level = y;
m_cp = z;
}
string name() const {
return m_name;
}
int level() const {
return m_level;
}
double cp() const {
return m_cp;
}
void display_stats() const {
cout << m_name << " stats are:" << endl;
cout << " " << "Attack: 2716.05" << endl;
cout << " " << "Defense: 1629.63" << endl;
cout << " " << "HP: 1086.42" << endl;
}
};
ostream& operator<<(ostream& os, const Pokemon &c) {
os << "The level of " << c.m_name << " is " << c.m_level << " with cp of " << c.m_cp;
return os;
}
And then change main() to look more like this:
int main()
{
Pokemon a;
Pokemon b("Weezing");
Pokemon c("Nidoking", 100);
Pokemon d("Mewtwo", 50, 5432.1);
cout << a << endl;
cout << b << endl;
cout << c << endl;
cout << d << endl;
cout << "Jessie: You are no match to me! Go " << b.name() << "!" << endl;
cout << "Gary: Go lvl " << c.level() << " " << c.name() << "! Crush them" << endl;
cout << "Ash: " << a.name() << " can do it even though he is only level " << a.level() << endl;
cout << "Jessie: Hahaha! My " << b.name() << " CP is " << b.cp() << endl;
cout << "Gary: " << c.name() << " CP is " << c.cp() << endl;
cout << "Ash: " << a.name() << " CP is " << a.cp() << endl;
cout << "Giovanni: Behold " << d.name() << " is here." << endl;
d.display_stats();
return 0;
}
Live Demo
This question already has answers here:
C++: Argument Passing "passed by reference"
(4 answers)
Why is the copy constructor called when we pass an object as an argument by value to a method?
(3 answers)
Closed 6 years ago.
The program should resolve grade 1 equations in this specific manner. I had to use output messages for the constructors and destructors for better understanding of the code.
#include "stdafx.h"
#include <iostream>
using namespace std;
class Ec {
public: float a, b; //equation's parameters
public:
Ec() {float x, y; cout <<"a= "; cin >> x; cout << "b= ";
cin >> y; a = x; b = y; cout << "void constr\n";};
Ec(float x, float y) { a = x; b = y; cout << "param constr\n"; }
~Ec() { cout << "destr\n"; }
Ec(Ec &z) { a = z.a; b = z.b; cout << "cpy constr\n"; }
friend float half1(Ec); //function to return a/2
friend float half2(Ec); //function to return b/2
friend float sol1(Ec); //function to return the solution for the standard eq
friend float sol2(Ec); //function to return the sol for the /2 param eq
};
float half1(Ec ec1) { return (ec1.a / 2);}
float half2(Ec ec1) { return (ec1.b / 2); }
float sol1(Ec ec1) { float x; return x = -ec1.b / ec1.a; }
float sol2(Ec ec1) { float x2; return x2 = -half2(ec1) / half1(ec1); }
int main()
{
int x, y;
cout << "a= ";
cin >> x;
cout << "b= ";
cin >> y;
Ec ec1;
Ec ec2(x, y);
Ec ec3 = ec1;
//the couts display for ex:" ec 2x+1=0 has sol -0.5"
cout << "ec " << ec1.a << "x+ " << ec1.b << "=0 has sol " << sol1(ec1) << endl;
cout << "ec " << ec2.a << "x+ " << ec2.b << "=0 has sol " << sol1(ec2) << endl;
cout << "ec " << ec3.a << "x+ " << ec3.b << "=0 has sol " << sol1(ec3) << endl;
cout << "ec halved " << half1(ec1) << "x+ " << half2(ec1) << "=0 has sol " << sol2(ec1) << endl;
cout << "ec halved " << half1(ec2) << "x+ " << half2(ec2) << "=0 has sol " << sol2(ec2) << endl;
cout << "ec halved " << half1(ec3) << "x+ " << half2(ec3) << "=0 has sol " << sol2(ec3) << endl;
}
return 0;
}
Now, I have troubles understanding why after the first cpy constructor(for ec3) another cpy constructor is called then a destructor. What is it doing?
Your functions take Ec objects by value
float half1(Ec ec1) { return (ec1.a / 2);}
^
These will make function local copies that are then destroyed at the end of each function.
If you want to avoid making these copies, pass the arguments by const reference
float half1(Ec const& ec1) { return (ec1.a / 2);}
^
I have just recently started class inheritance in c++. While I was making a "Test" program, a error occurred with the cout statement. No clue how to fix it and would be appreciate your response.
#include <iostream>
using namespace std;
class Power{
public:
void isWeak(){
cout << " Weak"<< endl;
}
void isStrong(){
cout << " Strong" << endl;
}
};
class Person:public Power{};
class Person2:public Power{};
int main(){
Person human;
Person2 human2;
cout << "Human is " << human.isWeak() << endl; //error
cout << "Human 2 is " << human2.isStrong() << endl; //error
system("pause");
return 0;
}
the main()'s cout statement has that error between the output and human
Change the functions to
char const *isWeak(){
return " Weak";
}
char const *isStrong(){
return " Strong";
}
As currently defined, both functions have void return type, which means the cout statements within main are trying to print void, which doesn't make sense, and is the cause of the error.
You are attempting to print a void:
cout << "Human is " << human.isWeak() << endl;
is the same as typing
cout << "Human is " << void << endl;
Which will not compile. What you need to do is define your functions in either of the following ways:
class Power
{
public:
std::string isWeak()
{
return std::string(" is weak");
}
std::string isStrong()
{
return std::string(" is strong");
}
};
Or, change your code:
cout << "Human is ";
human.isWeak();
cout << endl;
cout << "Human 2 is ";
human2.isStrong();
cout << endl;
Problem is with isWeak() and isStrong() return type. these two functions return void and you are trying to print it. you can try this-
cout << "Human is " ;
human.isWeak();
cout << endl;
cout << "Human 2 is " ;
human2.isStrong();
cout << endl;
You're trying to 'print' a 'void' statement in cout << "Human is " << human.isWeak() << endl;
You'll need to change your isWeak and isStrong functions to return a std::string/const char* or change the way you call them:
to string:
const char* isWeak() {
return " Weak";
}
// then you can do
cout << "Human is " << human.isWeak() << endl;
Or change the way you call the function:
cout << "Human is ";
human.isWeak();
Your isWeak and isStrong functions are void they do not return anything; calling cout << human.isWeak() is expecting isWeak to return something (an int, string, double, etc.).
I have overloaded a function fn as fn(int,char) & fn(int&,char&) as shown below:
#include <iostream>
using namespace std;
void fn(int a, char c);
void fn(int& a, char& c);
int main()
{
int a=10;
char c= 'c';
cout << "Inside main()" << endl;
cout << hex << "&a : " << &a << endl;
cout << hex << "&c : " << (int *)&c << endl;
static_cast<void(*) (int&, char&)> (fn)(a, c);
return 0;
}
void fn(int a, char c)
{
int tempInt;
char tempChar;
cout << "\n\nInside Call By Value Function " << endl;
cout << hex << "&a : " << &a << endl;
cout << hex << "&c : " << (int *)&c << endl;
cout << hex << "&tempInt : " << &tempInt << endl;
cout << hex << "&tempChar : " << (int *)&tempChar << endl;
}
void fn(int& a, char& c)
{
cout << "\n\nInside Call By Reference Function " << endl;
cout << hex << "*a : " << &a << endl;
cout << hex << "*c : " << (int*) &c << endl;
}
The resolution for call to fn(int,char) or fn(int&,char&) is made through cast static_cast<void(*) (int&, char&)> (fn)(a, c);
It gives output:
$ ./Overloading
Inside main()
&a : 0x22ac5c
&c : 0x22ac5b
Inside Call By Reference Function
*a : 0x22ac5c
*c : 0x22ac5b
Now when I put this in a class as below:
#include <iostream>
using namespace std;
class Test{
public:
void fn(int a, char c);
void fn(int& a, char& c);
};
int main()
{
int a=10;
char c= 'c';
Test T();
cout << "Inside main()" << endl;
cout << hex << "&a : " << &a << endl;
cout << hex << "&c : " << (int *)&c << endl;
static_cast<void(*) (int&, char&)> (T.fn)(a, c);
return 0;
}
void Test::fn(int a, char c)
{
int tempInt;
char tempChar;
cout << "\n\nInside Call By Value Function " << endl;
cout << hex << "&a : " << &a << endl;
cout << hex << "&c : " << (int *)&c << endl;
cout << hex << "&tempInt : " << &tempInt << endl;
cout << hex << "&tempChar : " << (int *)&tempChar << endl;
}
void Test::fn(int& a, char& c)
{
cout << "\n\nInside Call By Reference Function " << endl;
cout << hex << "*a : " << &a << endl;
cout << hex << "*c : " << (int*) &c << endl;
}
I get below error:
$ g++ -Wall Overloading.cpp -o Overloading
Overloading.cpp: In function ‘int main()’:
Overloading.cpp:23:42: error: request for member ‘fn’ in ‘T’, which is of non-class type ‘Test()’
How do I resolve this?
How to make a proper call for T's fn(int&,char&)
I guess in my code the expression static_cast<void(*) (int&, char&)> (T.fn)(a, c); is incorrect.
Please help.
Thanks
EDIT:
My mistake
editing Test T() to Test T;
gives error
$ g++ -Wall Overloading.cpp -o Overloading
Overloading.cpp: In function ‘int main()’:
Overloading.cpp:23:44: error: invalid static_cast from type ‘<unresolved overloaded function type>’ to type ‘void (*)(int&, char&)’
First: T is no variable, T is function, that returns Test and receive nothing.
Second: function-pointer is not member-function-pointer. You should use this syntax
typedef void (Test::*function)(int&, char&);
function f = &Test::fn;
(T.*f)(a, c);
See error: request for member '..' in '..' which is of non-class type: the problem is with Test T();, try ommiting the parentheses.
When you do Test T();, you're saying that T is a function with return type Test. However no such thing exists in your code.
The solution is:
Test T;