C++ :: Inheritance - Class Base Has No Member Function
Jan 16, 2014
I have a simple question about inheritance. Consider the following code:
Code:
Class Base {
int type;
Base(){};
};
Class Derived1 : public Base
[Code] ....
I get the following error: Class "Base" has no member "Function1";
That makes sense - as Base has not declared Function1. But how can I loop through a vector of Bases, and then if the object is of type Derived1, call the function Function1?
I have couple of objects which are using some amount of methods. Right now my application is not OOP and i am going to transfer it to OOP. So i would create each class for my each object. Almost all methods are in use for each object, the only one thing which is changing some of those objects passing not all parameters inside specific method. I can go two ways one is prepare interface for all methods i got and each of my classes could implement its own definition for it but other way almost all would implement exactly the same methods but with different parameters so maybe its better to create base class then for each object do inheritance from base class (polymorphism). inside base class i can prepare base methods and classes which will inherit from that class would override those methods for requirements they want.
So imagine: You got:
Memory CPU Latency
and all of them using mostly all of those same methods (only arguments for one of them could be use different way):
Base class: Stuff prop: name, id, date ... methods to ovveride: ExecuteQuery(), ExportToExcel() ...
classes: CPU, Memory, Latency (inheriting from Stuff) ovveride methods and align its definition for specific class use (remember mostly only passing args are used or not by specific class)
The most important thing is that those every objects mostly using all of those methods and what is diffrence that one object can use all of available parameters inside this method and other one no. What i should do? Go with interface or inheritance and polymporfizm inside base class?
I'm trying to implement a decision tree that gets doubles as input (in this sample code just random numbers) and returns a boolean value. At the nodes, I'd like to use various operators that can have different input and return types. I've implemented an abstract base class for the nodes and I'm deriving the specific nodes from it. In the code below, I've included only a few derived classes. Building the tree (randomly) is no problem. However, I'm looking for a clever way to evaluate the tree. I think that uncommenting the lines in bold print would in principle do it. However, this is not possible because "value" is not a member of the base class. The type of "value" is different in the derived classes, so I cannot simply declare it in the base class.
"Node.h" #pragma once class NodeBase{ public: NodeBase* Child_1; NodeBase* Child_2; virtual void evaluate() = 0;
I am trying to create a few subclasses which all use the base class constructor, according to my book this is all fine and dandy by using the "using Baseclass::Baseclass", this doesnt work for me.
class Monster { public: Monster(char[], char[], char[], int); //The constructor, and its implemented. etc.. };
[Code] ....
Yet, i recieve this error: error LNK2019: unresolved external symbol "public: __thiscall Human::Human(char * const,char * const,char * const,int)"
Why is this? Does doing it this way create const pointers somehow? The constructor works fine with Monster, i am using the same calls, just changed to create Human instead of Monster. All files are included where they should, monster and human are declared in the same header.
Basically I want to create a base class which defines a static data member so that its automatically redeclared as the same static data member in the derived class.
class A{ protected: static derivable int val; // A::val }
class B : public A{ // static derivable int val is already here // A::val AND B::val }
This seems impossible to me but I'm wondering if perhaps there's a way to add modifiers to the compiler to do this (or preferably something MUCH simpler)...
I have 2 classes with a Function with the same definition (both inherited from the same base class) and in my derived class I derive from both of those 2. Is it possible to use the Methods of both classes? for example with an other name?
class A { protected: int print(int a) { std::cout << "int A: " << a << std::endl; } }; class B : A
[Code] ....
is there something like using C::print as printc;?
The Problem, I have a Sprite class that derives from a Rectangle with properties Position, Size, Origin and Angle and a Text class that derives from Rectangle. Now i have a Button class deriving from both Sprite and Text.
- The Position, when moving the Button i have to change the position of both so i Implemented a new Method which calls SetPosition from the Sprite and the Text. - The SetSize just affects the Button so i just did using Sprite::SetSize; - The angle affects both so i just implemented a new Method and hide the other two
The problem is here: - The Origin: writing button.SetOrigin(-1,0) should set the Origin of the Button and writing button.SetTextOrigin should set the Origin of the text.
Should i just reimplement a Mehtod named SetTextOrigin and call Text::SetOrigin from there and hide the button.Text::SetOrigin or is there something like using Text::SetOrigin as SetTextOrigin?
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).
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() {
I just wondering if a base class can call the overridden function from a Derived class?
Here's an example:
//Base Class H class BaseClass { public: BaseClass(); virtual ~BaseClass(); virtual void functionA();
[Code] ....
So basically, when I am creating a new object of Derived class, it will initialize BaseClass and the BaseClass will call functionA but I want it to call the function overridden by Derived class.
I know that if I call newObj->functionA it will call the overridden function. Right now I want the base class to call the overridden function "this->functionA(); in BaseClass" during its initialization. Is it possible to do that?
Basically, I have a base class called MainShop and it has 3 derived classes which are SwordShop, SpellBookShop and BowShop. I want the base class to be able to call a function from one of the derived classes but no matter what i do, it doesn't seem to work!
I have two other derived classes, but its basically the same concept. I have a function in one of the derived classes and i would like to call it from the base class. This is one my derived classes:
I am working on a program for school and I have managed to get everything to work, except the last portion of the program. It seems that the program is not calling the last function EmployeeSummary. The program will compile, and will accept data and compute overtime pay, etc. Just won't go on to the last function.
#include <iostream> #include <string> #include <iomanip> using namespace std; class CPayroll {
When I call printStuff, the DerivedClass's function gets called. Now, if I remove the const part from the DerivedClass's printStuff function, we call the BaseClass's printStuff function.
I'm playing with the idea of a singleton base class, but I'm having an issue with how to implement the GetInstance() function in the base class. Since I'm trying to make this ridiculously simple for the child, I'd like to handle that in the base.
class Singleton { private: static Singleton* instance; Singleton() { Construct(); } // Private to avoid other instances
I am writing a program which is using SDL library. I have two different classes which one of them is Timer Class and the other is EventHandling Class.
I need to use some member functions and variables of Timer in some Eventhandling Class member functions, Although I want to define an object of Timer in int main {} and relate it to its member function that has been used in Eventhandling member function in order that it becomes easier to handle it, I mean that I want to have for example two objects of timer and two objects of Eventhandling class for two different users.
I do not know how to relate an object of a class from int main{} to its member function which is being used in another class member function.
I know if i will not use the pointer base class function "virtual double grossPay" will be called for both base class object and derived class object and when i will use pointer with reference to the object because base class function is virtual it will look for same function in derived class and if available it will execute it.
I mount a function (parameter - numeric vector; returns a string). However, this same function is used in several classes. To avoid that I keep duplicating the same code within these classes there is a way to do that as the code below?
std::string func( const vector<int> vec ) { //processamento return result; } class A {
I want to have a template function that is a member of a class. Is this possible? This code snippet is how I would think the syntax would go, although it doesn't compile. How would I achieve the same effect?
Code: class myclass { public: int member ; } ; template <typename T> void myclass::func( T& arg )
I keep getting an error saying ui.h:30: error: 'class BTree<Word>' has no member named 'prntInOrder' I have no line 30 in my ui.h but if i count the lines from the .cpp as if they were attached to the .h i find the call to the BTree printInOrder()
here is my ui.h
Code: #pragma once #include "btree.h" #include <fstream> #include <iostream> using namespace std;
[Code].....
As you can see the printInOrder() function is there so would it not see it?
Error:
Code: ui.h: In member function 'void UI::go(std::string)': ui.h:30: error: 'class BTree<Word>' has no member named 'printInOrder'
I have a class I am building called date and I've built all my functions to run and done all the necessary error checking in each function. However, my last and final function I have to write is where the # of days passed in as a parameter and then I have to increment the days by that many. And if the user does not increment any days at all in the parameter and leaves it blank, then it automatically increments by one day. I am having trouble with this. So for example if the user was to do this:
Date d1(10, 31, 1998); // Oct 31, 1998 Date d2(6, 29, 1950);// June 29, 1950
d1.Increment(); // d1 is now Nov 1, 1998 d2.Increment(5);// d2 is now July 4, 1950
The function starts out looking like this
void Date::Increment(int numDays = 1) { }
I know I have to use a for loop to accomplish this. I just don't know how to get it to where the days passed in will go to the next month and then days passed in would go to the next year.
Some background: I have a class, A, with members, B and C and D; I also have an array of A objects; I want to be able to have a function which takes said array and performs a certain calculation on either the B, C, or D members of each of the A objects, depending upon certain circumstances; I want to perform the same calculation regardless of which member is to be used in said calculation, such as always assigning the value 3 or multiplying the member's value by a cofactor of some sort.
My question, therefore, is: how I might do this using only one function be it a template or not?