C++ :: Two Classes Derived From Same Template Class And Operator

Mar 11, 2013

class A {
// is abstract
};

class B : public A {
// ...
};

[Code] ....
[Code] ....

main3.cpp: In member function ‘FooB& FooB::operator=(const FooC&)’:
main3.cpp:46:44: error: expected ‘(’ before ‘other’
main3.cpp:46:49: error: no matching function for call to ‘Foo<C>::Foo(const FooC&)’
main3.cpp:46:49: note: candidates are:
main3.cpp:19:2: note: Foo<T>::Foo() [with T = C]
main3.cpp:19:2: note: candidate expects 0 arguments, 1 provided
main3.cpp:16:25: note: Foo<C>::Foo(const Foo<C>&)
main3.cpp:16:25: note: no known conversion for argument 1 from ‘const FooC’ to ‘const Foo<C>&’

Is there any way to make it work?

View 1 Replies


ADVERTISEMENT

C++ :: Operator Overloading For Derived Classes

Jul 29, 2014

Code:

class Var {
public:
Var();
~Var();
private:
QMap<QString, QSharedPointer<Data>> _mapVars;
};
QDataStream &operator<<(QDataStream &stream, const QSharedPointer<Data> p_data);
QDataStream &operator>>(QDataStream &stream, Data &p_data)

I want to serialize _mapVars into a file. However, I have many other classes that are derived from Data, do i need to check for Data type inside the overloaded << functions like below in order to serialize ??? This doesn';t seem to be very correct ...

Code:
QDataStream &operator<<(QDataStream &stream, const QSharedPointer<Data> p_data) {
if(p_data->GetType == .....) {
}
}

View 7 Replies View Related

C++ :: Derived Class Inherit Operator Of Base Class

Jan 17, 2012

If Yes, then why this syntax does not works :

class Derived : public Base {
public:
Derived& operator=(const Derived &rhs) {
operator =(static_cast<const Base&>(rhs));

[Code] ....

View 2 Replies View Related

C++ :: Create Base Class That Is Derived (inherited) By Three Other Classes?

Apr 30, 2013

how to create a base class that is derived (inherited) by three other classes?

View 12 Replies View Related

C/C++ :: Template Class Function Taking A Derived And Generic Object

Feb 23, 2014

I'm trying to write a function for receiving messages, so my classes can communicate with each other. The only issue I get is a compile error asking me to define the base parameter as one of the derived instances. I tried using a void* to fill the need, but then I lose the initial type, which I need to check for. How might I go about writing a generic object for this?

Here's my code:

template<class Object>
class State
{
public:

[Code].....

Should I just have all of the objects inherit in the order of Object >> GenericObject >> DerivedObject?

View 10 Replies View Related

C++ :: How To Use The Operator In Class Template In Program

Apr 25, 2013

I have a list of numbers in an array created by a class template

Code:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
const int CAPACITY = 20;
template <class T>
class A

[code].....

View 2 Replies View Related

C++ :: Error Overloading Operator / Class Template Vector

Feb 7, 2013

I'm trying to implement a vector class in C + +. However when I go to actually run the main, it generates an interrupt operating system.

MAIN.cpp
#include "header.h"
int main() {
// Definisco le istanze delle classi
vettore <int> vet;
vettore <int> vet2;
vettore <int> vet3;

[code].....

View 7 Replies View Related

C++ ::  Overloaded Assignment Operator For Class Template Objects?

May 23, 2013

I designed a class template to create unique arrays. I was able to successfully input data to and output data from my array objects, irrespective of the datatype. However, I can't for the life of me fathom why my overloaded assignment operator worked perfectly well only for integer datatype and not for double/string datatypes.

Here is the class definition:

template <class dataType>
class myArray {
public:
void setArrayData();

[code]....

And here is the definition of the overloaded assignment operator:

template<class dataType>
const myArray<dataType>& myArray<dataType>::operator=(const myArray<dataType>& rightArray) {
int i;
if(this != &rightArray) {
delete [] arrayPtr;

[Code] ....

And here is my main function that tests the operations on objects of the class:

int main(){
//object declarations
myArray<double> list(5); //a single-parameter object declaration of class myArray
myArray<double> myList(2,13); //a two-parameter object declaration of class myArray

[code]....

The problem I'm having starts from where the assignment operator is being tested: for double and string datatypes, the upper input/output section works fine, but the assignment section freezes the display until the program execution is manually terminated!

View 19 Replies View Related

C++ :: Namespaces / Classes - Perform Operator Overload With Insertion Operator

Mar 22, 2013

I'm doing a refresher for C++ and have gotten to operator overloading. I'm trying to perform an operator overload with the insertion (<<) operator, but I have encountered a problem.

Here's my class [In a header file "Shinigami.h"]

#include<string>
namespace K{
class Quincy;
class Shinigami{
friend std::ostream& operator<<(std::ostream&, const Shinigami&);

[Code] .....

If the operator function is a friend of the 'Shinigami' class, why doesn't it recognize any of it's private members? I need it to be in this file because I'm doing a bit of association with the 'Quincy' class.

I thought it was the namespace, but I included that.

View 9 Replies View Related

C++ :: Derived Classes From DLL

May 5, 2013

I've created a base DLL for all my future DLL's, a way of getting version numbers and such and that compiles fine, but I can't add it into a class for a new DLL. All the headers do have an appropriate cpp to define the function declarations (and they compile fine).

All for the base DLL I have:

LibVer.h
Version.cpp
Function.cpp

LibVer.h

#pragma once
#include <vector>
#define DLLEXPORT 1
#define DLLIMPORT 2
#define DLL DLLIMPORT

[Code] .....

View 6 Replies View Related

C++ :: Constructors In Derived Classes

Apr 7, 2014

As long as no base class constructor takes any arguments, the derived class need not have any constructor, if one or more arguments are used then it is mandatory for the derived class to have a constructor and pass the arguments to base class constructors. While applying inheritance, we usually create objects using derived class. Then it makes sense for the derived class to pass arguments to the base class constructor. When both the base and derived class contain constructors ,the base class constructor is execute first.

In case of multiple inheritance, the base classes are constructed ,in the order in which they appear in the declaration of the derived class. Similarly in a multiple inheritance the constructors will be executed in order of inheritance. Since the derived class takes the responsibility to supply initial values to the base class,we supply the initial values that are required by all the classes together where the derived class object is declared.

The constructor of the derived class receives the entire list of values of arguments and pass them on to the base constructors int the order in which they are declared in the derived class

View 1 Replies View Related

C++ ::  How To Overload Variables In Derived Classes

Apr 6, 2013

Is it possible to overload a variable in a derived class? Example:

struct Circle
{
int radius() const { return r; }
private:
int r;
}
struct Smiley : Circle
{
// inherits the function int radius() const, but doesn't return Smiley::r
private:
int r;
}

View 7 Replies View Related

C++ :: Overloading Stream Operators On Derived Classes

Apr 16, 2014

S I want to have different >> operators for several derived classes. Has I tested...

Code: class base{
friend std::istream & operator>>(std::istream & in, base & v);
public:
base();

[Code]......

I noticed that the base operator is the only one being called for all 3 objects. which makes sense and now that I think about it I am more surprised that the "derived operators" compiled at all.

Still, how would I go about declaring different overloaded operators for the different classes?

View 4 Replies View Related

C/C++ :: Access Derived Classes Functions On A Vector

Jan 19, 2014

I need to access the functions of the derived classes from a vector of objects of base classes (can't believe I wrote it). Here a Diagram for you to understand:

So as you see, I need the function Use() from the Usable class, to be able to be called from the vector like:

inventory.at(x)->Use()

View 14 Replies View Related

Visual C++ :: Destroying CArray Of Cobject Derived Classes

Nov 21, 2013

I use a CArray<CClase1,CClase1> m_Clases1.

CClase1 is derived of CObject. " class CClase1 : public CObject"

When, at last I do : "m_Clases1.RemoveAll()" , I suppose that the CClase1 destructor is called. But when i do this the program fails.

View 6 Replies View Related

C++ :: Use Of Template Classes In Other Template Classes

Jan 24, 2012

I'm trying to make a template class which holds a template list I made. The list consists of template nodes (another template class I made).

My problem is that in some places (not everywhere) the compiler says that I'm trying to access private members of the node's class.

For example, if I define my node like this:
template<class T>
class CoefNode {
private:
T coef;

[Code]....

View 5 Replies View Related

C++ :: Overloading Operator Of Inherited Classes

Apr 19, 2013

I have a class A, from which three classes Aa Ab and Ac are inherited. In class A I have defined some functions by virtual foo()=0, which I implemented in each subclass. Each class is written in a separated .h and .cpp file.

However, now I think it is possible to overload the operator+ INSIDE each class (including pure virtual in class A), such that something like

int main() {
A *value = new Aa();
A value2 = *value + 1.0f;
}

This should be realizable, because the operator+ is part of the Aa class. Now, I would like to do something like

int main() {
A *value = new Aa();
A value2 = 1.0f + *value;
}

This time, I expect I cannot overwrite the operator+, because it is not part of either class A or class Aa.

View 7 Replies View Related

C++ :: Calling Derived Class Functions In A Function With Parameter Of Base Class

Mar 30, 2013

Say I have 3 classes:

class Player {
public:
virtual func1();

[code]....

Say in my main class, I have a function fight(Player p1, Player p2) and I would like to do something like this in the fight function, given that p1 is the human and p2 is the computer:

//function fight()
fight(Player p1, Player p2) {
p1.func2();
}
//using function fight()
fight(human, computer);

When I compile the program, I got this: error: ‘class Player’ has no member named 'func2()' What can I do to allow p1 to call func2 inside fight()? I'm not allowed to use pointers as the parameter for fight() and have to use the signature fight(Player p1, Player p2).

View 6 Replies View Related

C++ :: Size Of Derived Class With Overriding Virtual Functions From Base Class?

Jan 21, 2014

The compiler creates virtual table for the base class and also for the derived class whether we override it or not.

That means each class has separate virtual table. when we get the size of the each class with out any data members... the size of base is -- 4 bytes(64 bit) and the size of derived is -- 1

The size of base class 4 is correct since it creates the virtual pointer internally and its size is member data + virtual pointer, but it in this case I have included any data members so it has given 4 byts.

But why in case of derived is 1 byte, since it the derived class has overridden the virtual function from base, this will also contains the virtual pointer which will be pointing to derived class Vtable, it the size of the class suppose to be 4 instead of 1 byte.

#include<iostream>
class A{
public:

[Code].....

View 1 Replies View Related

C++ :: Transfer Values Set In Privates Of Base Class By Object Of One Derived Class To Another

Apr 26, 2014

I have my main.cpp like this:

#include <iostream>
#include "curve1.h"
#include "curve2.h"
using namespace std;
int main() {
Curve1 curve1Obj;
Curve2 curve2Obj;

[Code]...

Base class Score has two derived classes Curve1 and Curve2. There are two curve() functions, one is in Curve1 and other in Curve2 classes. getSize() returns the value of iSize.

My base class header score.h looks like this:

#ifndef SCORE_H
#define SCORE_H
class Score {
private:
int *ipScore;
float fAverage;
int iSize;

[Code]...

You can see that I have used curve1Obj to enter scores, calculate average and output. So if I call getSize() function with cuve1Obj, it gives the right size that I took from user in enterScores() function. Also the result is same if I call getSize() in score.cpp definition file in any of the functions (obviously).
.....

The problem is when I call curve() function of Curve2 class in main (line 23) with the object curve2Obj, it creates a new set of ipScore, fAverage and iSize (i think?) with garbage values. So when I call getSize() in curve() definition in curve2.cpp, it outputs the garbage. .....

How can I cause it to return the old values that are set in curve1.cpp?

Here is my curve2.cpp

#include <iostream>
#include "curve2.h"
using namespace std;
void Curve2::curve() {
cout << "getSize() returns: " << getSize() << endl; // out comes the garbage
}

Can I use a function to simply put values from old to new variables? If yes then how?

View 3 Replies View Related

C++ :: Deleting Array Of Derived Class Objects Through Base Class Pointer

Mar 21, 2015

In this book, item 3 is about never treat arrays polymorphically. In the latter part of this item, the author talks about the result of deleting an array of derived class objects through a base class pointer is undefined. What does it mean? I have an example here,

Code:
class B
{
public:
B():_y(1){}
virtual ~B() {
cout<<"~B()"<<endl;

[Code] ....

This sample code does exactly what I want. So does the author mean the way I did is undefined?

View 1 Replies View Related

C++ :: Creating Array Of Pointers To Base Class To Point To Derived Class Objects Dynamically

Jan 16, 2013

Please consider the following code :

#include <iostream>
using namespace std;
class superclass;
class subclass1;
class subclass2;

[Code] ....

As you can see I want to create a dynamically allocated storage of references to a parent class each of which can then point to a child class, how ever I do not know how to extract the child class out again from that array so i may access its variable b.

View 2 Replies View Related

C++ :: Declaration Of Template Specialized Classes

Aug 12, 2014

class intClass{};
class stringClass{};

template <typename T>
class Mediator {
// bunch of code
};

I want Mediator<int> to declare intClass friend, Mediator<std::string> to declare stringClass friend etc..., without having to redefine the entire specialization

template<>
Mediator<int>::friend intClass;

View 5 Replies View Related

C++ :: Template Classes And Inline Methods

Aug 17, 2012

I have a template class which defines a few heavy methods. For now, they are defined in the same .h file as the class definition, but i`d like to have them in a separate .cpp file.

A situation i find you describe in the FAQs arises: [URL] ....

Problem: the export keyword has been deprecated in c++0x, if i recall correctly, and has never been implemented in any of the compilers i am using (msvc, gcc).

#Including the the .cpp file after the class definition (as described in the second post of the FAQ) works.

another question: i have methods that dont use any template code. Can i somehow declare them as such? (more of an esthecial question, which would make it easier to distinguish between template and non.template code).

View 6 Replies View Related

C++ :: Derived Class Not Overwriting Base Class Function - Using Vectors

Feb 4, 2014

So I have a base class, lets call it base. In base I have a virtual function called update(), update just couts "base" then I have a class derived from base called derived;

it has a function called update(), update just couts "derived" then I create a vector called Vec it's initialised like this:

std::vector<base> Vec;

then I add an element into it like this

Derived DerElement;
Vec.push_back(DerElement);

then when I type:

for (int i=0; i<Vec.size(); i++) {
Vec.at(i).Update();
}

It outputs:

Derived DerElement2;
DerElement2.Update();

and it outputs this:

#include <iostream>
#include <vector>
class Base {
public:
virtual void Update() {

[Code] .....

and this is it's output:

Base
Derived
Press any key to continue . . .

View 1 Replies View Related

C++ :: Friendship From Derived Class Method To Base Class Members

Jul 15, 2014

I would like to know if there's a way to make a method from a derived class a friend of its base class. Something like:

class Derived;
class Base {
int i, j;
friend void Derived::f();
protected:
Base();

[Code] ......

View 3 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved