I'm trying to overload operator "<<" in c++. I have a class called TCalendario, with 3 privates int attributes, and 3 public getters to return this variables in public mode, but when I try to save the return value of the getter in another int variable, i can't compile. The mensage error is:
"cannot convert 'TCalendario::Dia' from type 'int (TCalendario::)()' to type 'int'"
Private attributes:
private:
int dia, mes, anyo
And getters:
int TCalendario::Dia()
{
return dia;
}
int TCalendario::Mes()
{
return mes;
}
int TCalendario::Anyo()
{
return anyo;
}
The code that crashes when I try to compile is:
ostream& operator<<(ostream &x, TCalendario &c)
{
int day=c.Dia;
int month=c.Mes;
int year=c.Anyo;
}
In order to call a method without arguments, you need to put empty braces, like this:
int day=c.Dia();
You missed the () in function call
int day=c.Dia();
int month=c.Mes();
int year=c.Anyo();
Related
I want to build some program in C++, but I'm quite newbie in OOP and I meet a problem that Google can't help me for right now. I think that the C++ is in version 14, but I'm still not sure how to check this in VS2017. I want to use thevalues passed to the class' constructor as default values for another method of the same class.
Specially for my problem I build simple case program for adding two int's, which is in three separate files and it looks like this:
main.cpp
#include <iostream>
#include "add_this.h"
using namespace std;
int main(int argc, char** argv)
{
Add plus(1, 2);
cout << plus.AddingResult(IF EMPTY, TAKE VALS 1 AND 2 FROM ABOVE plus) << endl;
return 0;
}
add_this.cpp
#include "add_this.h"
Add::Add(int a, int b)
{
}
Add::~Add()
{
}
int Add::AddingResult(int a, int b)
{
return a + b;
}
add_this.h
#pragma once
class Add
{
private:
int a;
int b;
public:
Add(int a, int b);
~Add();
int AddingResult(int a, int b);
};
Ok, if in main.cpp I pass values by hand in example plus.AddingResult(2, 3) I will get 5. My problem is that I want to get method, which will take b or a and b values (or in super method version will take b by hand and pass a from constructor). I try a lot of approaches related with pointers or defining Set and Get methods but all fails in my implementation - I don't say that they are wrong I say that my implementations was wrong. I wont attache them for question clarity.
First, let's give your identifiers better names:
class Adder
{
int lhs_;
int rhs_;
public:
Adder(int lhs, int rhs);
~Adder();
Then we define an overload set for the member function add as we cannot define arguments with non-static member variable as their default value:
int add(int lhs, int rhs);
int add(int lhs);
int add();
};
Then, we need to "save" the values given to Adder's constructor:
Adder::Adder(int lhs, int rhs)
: lhs_(lhs), rhs_(rhs) // 1
{}
The line marked // 1 is the syntax for the member-initializer-list; it initializes the member variables lhs_ and rhs_ with the argument's values. Last step, we use those variables in our overload set:
int Adder::add(int lhs, int rhs)
{
return lhs + rhs;
}
int Adder::add(int lhs)
{
return add(lhs, rhs_);
}
int Adder::add()
{
return add(lhs_, rhs_);
}
Full demo: https://coliru.stacked-crooked.com/a/e08b8860c20d53c9
Why is my overloaded member function only "ambiguous" as a char and not an int and string?
I'm trying to create a one-code path for my Char class by funneling code through an overloaded equals() function. It works fine when I use equals as an int and string but is ambiguous as a char.
class Char
{
private:
char cData;
int iData;
string sData;
public:
//Mutators:
void equals(char c);
void equals(int c);
void equals(string c);
//Constructors:
Char(char c);
Char(int c);
Char(string c);
};
void Char::equals(char c)
{
cData = c;
}
void Char::equals(int c)
{
iData = c;
}
void Char::equals(string c)
{
sData = c;
}
Char::Char(char c)
{
this->equals(c); //Call to member function 'equals' is ambiguous
}
Char::Char(int c)
{
this->equals(c);
}
Char::Char(string c)
{
this->equals(c);
}
The error only happens for char, which is confusing since string works fine. I expected it to work for all of them since that's been the case so far.
It's ambiguous because if you do
Char c(42);
The compiler does not know whether it should call the char or int constructor. Both are an equally good match.
The same goes for equals(123);. Again, both the char and int overloads match and the compiler cannot tell which one you intend to call.
you can use a single equal method to accept a char or an int. like
void euqals(unsigned int c_i);
This is my situation:
class Filter3by3 {
public:
virtual inline Mat convolution((Mat & mat, int i, int j, int rows, int cols) {
code
}
};
class MySobel: public Filter3by3 {
public:
inline Vec3b convolution(Mat & mat, int i, int j, int rows, int cols) {
code
}
};
Now, when I call:
Filter3by3 f = choose_filter(filtername); // Returns a Sobel filter
Mat mat;
s.convolution(args);
The base class method is called.
I am quite newbie at c++ method binding rules, so can you tell me where I am wrong?
I appreciate your help.
UPDATE
It appears that even with
virtual inline Mat convolution((Mat & mat, int i, int j, int rows, int cols)
It does not work.
This is a running program, compiled with g++ -std=c++11
#include <iostream>
using namespace std;
class Filter {
public:
Filter() { }
virtual int ehi() {
cout << "1" << endl;
return 1;
}
};
class SubFilter : public Filter {
public:
SubFilter() : Filter() { }
int ehi() {
cout << "2" << endl;
return 2;
}
};
Filter choose_filter(){
SubFilter f;
return f;
}
int main(int argc, char* argv[]) {
Filter f = choose_filter();
f.ehi();
return 0;
}
It prints 1 instead of 2. I used virtual to ensure dynamic binding, but it does not seem to be enough, also with "override" keyword.
An overridden method has to have the same signature, i.e. argument and return types, as the base method. The compiler can notify you if these do not match if you add the override keyword to the signature.
When you assign an object of derived class to an object of base class like that, you not achieving dynamic dispatch, you achieve slicing (all of the additional data members of SubFilter are lost)
Filter choose_filter(){
SubFilter f;
return f;
}
Instead you should pass it by (safe) pointer or reference, like this:
std::shared_ptr<Filter> choose_filter(){
return std::make_shared<SubFilter>();
}
int main(int argc, char* argv[]) {
auto f = choose_filter();
f->ehi();
return 0;
}
There's a keyword in c++ called override. It exactly solve the problem you mentioned:
struct MySobe l: Filter3by3 {
inline Vec3b convolution(Mat & mat, int i, int j, int rows, int cols) override { code }
};
The presence of the override ensure that the method really overrides the base class method.
In your code, it will cause a compilation error because the derived class does not overrides, since the signature are different.
I've this error when I try to save a number into my vector...
Invalid types ‘<unresolved overloaded function type>[int]’ for array subscript
The code is:
class Elemento{
private:
int Nodo;
public:
Elemento(){};
~Elemento(){};
void SetNumero(int x) { Nodo = x; };
int GetNumero() { return Nodo; };
};
class MagicSquare{
private:
int N;
int Possibili_N;
int Magic_Constant;
vector<Elemento> Square(int Possibili_N);
public:
MagicSquare() { };
~MagicSquare() { };
void Set_N(int x) { N = x; };
void Set_PossibiliN(int x) { Possibili_N = x; };
void Set_MagicConstant(int x) { Magic_Constant = x; };
. . .
void SetSquare(int i, int x) { Square[i].SetNumero(x); }; // got error here
int GetSquare(int i) { return Square[i].GetNumero(); }; // got error here
};
I've got error whenever I use Square[i].method()...
I call a method that pass the index in the Square and the value to put in Elemento->Nodo, but I've to use a public method to access to private Nodo. The same with the GET. I want to get the value for displaying it.
You seem to have declared Square as a function, not a variable.
Instead, declare vector<Elemento> Square; and initialize it in the constructor.
You declared Square as a function, not a variable. So Square[i] is not valid.
Change
vector<Elemento> Square(int Possibili_N);
to
vector<Elemento> Square;
or call it using
Square(i)
if it is actually a function.
If you change it to a variable, you need to be sure to initialize it properly, preferably in the constructor.
Your line vector<Elemento> Square(int Possibili_N); is know as C++ most vexing parse.
Instead of declaring a member variable, as intended, you are declaring a function taking an int and returning a vector.
Instead, setup the member vector (and all other member variables) in the constructor initialization list:
class MagicSquare{
private:
int N;
int Possibili_N;
int Magic_Constant;
vector<Elemento> Square;
public:
MagicSquare( int n, int p, int m ) :
N( n ),
Possibili_N( p ),
Magic_Constant( m ),
Square( p ) {
}
...
Hello guys i had this error while i was compiling
error:'unisgned int vehicle::accelerate' is not a static member of 'class vehicle'
Any idea how to fix this?
Header file
class vehicle
{
public:
enum Switch
{
SWITCH_ON=0,
SWITCH_OFF
};
vehicle();
~vehicle();
bool powerSwitch(Switch );
unsigned int accelerate(unsigned int );
unsigned int decelerate(unsigned int );
bool isMoving();
unsigned int getSpeed();
unsigned int setSpeed(unsigned int);
private:
unsigned int speed;
bool moving;
};
vehicle.cpp
unsigned int vehicle::accelerate(amount)
{
if(moving==true;){
speed+=amount;
}
return speed;
}
You are missing the type in the parameter list:
unsigned int vehicle::accelerate(unsigned int amount)
{
.....
}
As you have declared:
unsigned int accelerate(unsigned int );
So you must implement:
unsigned int vehicle::accelerate(unsigned int amount)
{
//...
The type needs to be given again at this point.
The error might be somewhere else, where you're trying to access the accelerate member not using the operator -> or ., but ::, apart from forgetting the type of the parameter