I cant understand why I have redefinition trying to run this example. Can anyone tell me?
using namespace std;
class Base {
protected: int *value;
public: Base() {
value = new int,
*value = 1;
};
Base(int &n) {
value = new int[n];
}
};
int main() {
int x=2;
Base zm;
Base(x);
system("Pause");
}
Witaj Przemeku Na StackOverflow!
How about this?:
class Base {
protected: int *value;
public:
Base() {
value = new int,
*value = 1;
};
Base(int &n) {
value = new int[n];
};
};
int main()
{
int x;
x = 2;
Base base;
base = Base(x); <--- fix
return 1;
}
Proszę bardziej formatować kod! ;)
Related
I can't figure it out why the Derived class remains Abstract after overriding function fun().
Here is the error message:
error: invalid new-expression of abstract class type 'Derived' Base *t = new Derived(a);
error: no matching function for call to 'Base::fun(int&)'int i = t->fun(b);
#include <iostream>
using namespace std;
class Base
{
protected:
int s;
public:
Base(int i = 0) : s(i) {}
virtual ~Base() {}
virtual int fun() = 0;
};
class Derived: public Base
{
public:
Derived(int i) : Base(i) {}
~Derived() { cout << --s << " "; }
int fun(int x) { return s * x; }
};
class Wrapper
{
public:
void fun(int a, int b)
{
Base *t = new Derived(a);
int i = t->fun(b);
cout << i << " ";
delete t;
}
};
int main()
{
int i, j;
cin >> i >> j;
Wrapper w;
w.fun(i, j);
return 0;
}
The function has two different signatures between the base class and derived class
virtual int fun() = 0;
but then the derived class
int fun(int x) { return s * x; }
if you would add override it would alert you to this mistake
int fun(int x) override { return s * x; }
The problem is that
int fun(int x) { return s * x; }
does not override
virtual int fun() = 0;
because the argument list is different (no arguments vs. a single int). If you'd written
int fun(int x) override { return s * x; }
as you should since C++11, then the compiler would have given you an error about this.
I am working with conversion operators and I an error just popped out of nowhere. C class is derived from B and has no relation with class A, however, debugger shows that when doing C* val1 = new C val1 is showed as C* {A<B>}. It also produces an error because that A in the C* pointer has a size of an unreasonable size (it gives a different size each time it is executed so I just suppose it gets a number from another application).
#include <iostream>
#include <vector>
template<typename widget_type>
class A
{
public:
std::vector<widget_type*> value;
virtual ~A() {}
void Add(widget_type* val)
{
value.push_back(val);
}
template<typename return_type>
operator A<return_type>()
{
unsigned int size = this->value.size();
std::vector<return_type*> return_value;
return_value.reserve(size);
for (unsigned int i = 0; i < size; i++)
{
return_value[i] = dynamic_cast<return_type*>(this->value[i]);
}
A<return_type> target;
target.value = return_value;
return target;
}
};
class B
{
public:
virtual ~B() {}
};
class C : public B
{
public:
void Print()
{
std::cout << "C CALL\n";
}
};
class D : public B
{
};
int main()
{
std::cout << "Start!\n";
A<C> source;
C* val1 = new C;
source.Add(val1);
A<B> target = source;
A<B>* target2 = dynamic_cast<A<B>*>(&source);
std::cout << "END\n";
}```
for (unsigned int i = 0; i < size; i++)
{
return_value[i] = dynamic_cast<return_type*>(this->value[i]);
}
You are invoking undefined behaviour by accessing return_value[i] on an empty vector.
Program works but I am not sure what is wrong with constructor since every time program runs it gets this error "warning: base class 'Alat' is uninitialized when used here to access 'Alat::ime' [-Wuninitialized]". I suppose it's something wrong how I called a constructor from base class but I am not sure what is problem. Really need help, tnx in advance.
#include <iostream>
#include <string>
using namespace std;
class Alat{
protected:
string ime;
int serBr;
int cena;
public:
void setIme(string i);
string getIme();
void setSerBr(int sb);
int getSerBr();
void setCena(int c);
int getCena();
Alat();
Alat(string i, int sb, int c)
:ime(i),
serBr(sb),
cena(c)
{}
void info();
~Alat();
};
#include "Alat.h"
class Rucni : public Alat{
protected:
int minGodKor;
public:
Rucni():Alat(ime, serBr, cena) //I think here is problem, is it wrong called?
{}
int getminGodKor();
void setminGodKor(int min);
void info();
~Rucni();
};
Let the child default constructor call the default parent constructor, and create another child constructor with parameters to call the corresponding one of the parent:
#include <string>
using std::string;
class Alat
{
protected:
string ime;
int serBr;
int cena;
public:
void setIme(string i)
{
ime = i;
}
string getIme()
{
return ime;
}
void setSerBr(int sb)
{
serBr = sb;
}
int getSerBr()
{
return serBr;
}
void setCena(int c)
{
cena = c;
}
int getCena()
{
return cena;
}
Alat()
{
}
Alat(string i, int sb, int c) : ime(i), serBr(sb), cena(c)
{
}
~Alat()
{
}
};
class Rucni : public Alat
{
protected:
int minGodKor;
public:
Rucni() // implicit call of the parent default constructor
{
}
Rucni(string i, int sb, int c) : Alat(i, sb, c) // explicit call of the corresponding parent constructor
{
}
int getminGodKor()
{
return minGodKor;
}
void setminGodKor(int min)
{
minGodKor = min;
}
~Rucni()
{
}
};
int main()
{
Rucni r;
return 0;
}
namespace iris {
namespace imon {
class myclass {
private:
typedef enum ppTag {
X1 = 0,
X2 = 1,
X3 = 254,
X4 = 255
} pp;
typedef struct {
int x;
int y;
int z;
} Data;
pp myFunc();
public:
myclass() { };
virtual ~myclass() {};
int func();
};
pp myclass::myFunc()
{
...
}
int myclass::func()
{
return 0;
}
}
}
g++ returns error: pp does not name a type
I thought I can easily use privately declared structures, typedefs etc. within public methods of the class. What else am I doing wrong?
You need to qualify the type in order to access it:
myclass::pp myclass::myFunc()
{
...
}
I'm trying to compile and run this program .obviously, it doesn't work!my question is why it is invalid conversion from bottom** to lefta** while bottom* can convert into lefta* ?
#include<iostream>
using namespace std;
class top
{
private:
int a;
public:
top(int b):a(b){}
virtual void output(){cout<<a<<endl;}
};
class lefta:virtual public top
{
private:
int b;
public:
lefta(int c,int d):top(c),b(d){}
void output(){cout<<b<<endl;}
};
class righta:virtual public top
{
private:
int c;
public:
righta(int c,int d):top(c),c(d){}
void output(){cout<<c<<endl;}
};
class bottom:public lefta,public righta
{
private:
int d;
public:
bottom(int e,int f,int g,int h):top(e),lefta(e,f),righta(e,g),d(h){}
void output(){cout<<d<<endl;}
};
int main()
{
bottom* bo=new bottom(1,2,3,4);
// lefta* le=bo;
// le->output();
bottom** p_bo=&bo;//here
lefta** p_le=p_bo;//here
(*p_le)->output();
return 0;
}
class leftb : public lefta { /* blah */ };
bottom* bo = new bottom(1,2,3,4);
bottom** p_bo = &bo;
lefta** p_le = p_bo;// let's pretend it works
// now p_le points to the variable bo, which is of type bottom*
// so *p_le is a reference to the variable bo
*p_le = new leftb(1,2); // wait, did we just assign a leftb* to a bottom*?
// (and yeah, I'm leaking memory. Sue me)
// bo now points to a leftb, but it is a bottom*
// oops, we just broke the type system