no 'void TestSub::print()' member function declared in class 'TestSub' - c++

parent class
class Test {
public:
Test(){};
virtual ~Test(){};
void print() { cout<<1<<endl;};
};
sub class .h define
class TestSub: public Test {
public:
TestSub();
virtual ~TestSub();
};
sub class .cpp implements
#include "TestSub.h"
TestSub::TestSub() {
}
TestSub::~TestSub() {
}
void TestSub::print(){
cout<<2<<endl;
}
int main(){
TestSub *t=new TestSub();
t->print();
}
why:
..\src\TestSub.cpp:17:21: error: no 'void TestSub::print()' member function declared in class 'TestSub'

You have 2 errors:
First you have to declare you function in TestSub as : void print();
Second you have to specify a return type for you implementation, C++ do not accept default return type such as C, so you must convert your implementation to void TestSub::print() {...}

print() funciton is not declared in TestSub class.
class TestSub: public Test {
public:
TestSub();
void print(); // add declaration.
virtual ~TestSub();
};
I guess you also intended to make Test::print virtual?
class Test {
public:
Test(){}
virtual ~Test(){}
virtual void print() { std::cout << 1 << std::endl;}
};

If you don't specify the return type C defaults to int as return type of a function. Which doesn't match the void return type of the declaration in the class.

Related

How can I access these functions on the derived class? [duplicate]

How do I call the parent function from a derived class using C++? For example, I have a class called parent, and a class called child which is derived from parent. Within
each class there is a print function. In the definition of the child's print function I would like to make a call to the parents print function. How would I go about doing this?
I'll take the risk of stating the obvious: You call the function, if it's defined in the base class it's automatically available in the derived class (unless it's private).
If there is a function with the same signature in the derived class you can disambiguate it by adding the base class's name followed by two colons base_class::foo(...). You should note that unlike Java and C#, C++ does not have a keyword for "the base class" (super or base) since C++ supports multiple inheritance which may lead to ambiguity.
class left {
public:
void foo();
};
class right {
public:
void foo();
};
class bottom : public left, public right {
public:
void foo()
{
//base::foo();// ambiguous
left::foo();
right::foo();
// and when foo() is not called for 'this':
bottom b;
b.left::foo(); // calls b.foo() from 'left'
b.right::foo(); // call b.foo() from 'right'
}
};
Incidentally, you can't derive directly from the same class twice since there will be no way to refer to one of the base classes over the other.
class bottom : public left, public left { // Illegal
};
Given a parent class named Parent and a child class named Child, you can do something like this:
class Parent {
public:
virtual void print(int x);
};
class Child : public Parent {
void print(int x) override;
};
void Parent::print(int x) {
// some default behavior
}
void Child::print(int x) {
// use Parent's print method; implicitly passes 'this' to Parent::print
Parent::print(x);
}
Note that Parent is the class's actual name and not a keyword.
If your base class is called Base, and your function is called FooBar() you can call it directly using Base::FooBar()
void Base::FooBar()
{
printf("in Base\n");
}
void ChildOfBase::FooBar()
{
Base::FooBar();
}
In MSVC there is a Microsoft specific keyword for that: __super
MSDN:
Allows you to explicitly state that you are calling a base-class implementation for a function that you are overriding.
// deriv_super.cpp
// compile with: /c
struct B1 {
void mf(int) {}
};
struct B2 {
void mf(short) {}
void mf(char) {}
};
struct D : B1, B2 {
void mf(short) {
__super::mf(1); // Calls B1::mf(int)
__super::mf('s'); // Calls B2::mf(char)
}
};
If access modifier of base class member function is protected OR public, you can do call member function of base class from derived class.
Call to the base class non-virtual and virtual member function from derived member function can be made.
Please refer the program.
#include<iostream>
using namespace std;
class Parent
{
protected:
virtual void fun(int i)
{
cout<<"Parent::fun functionality write here"<<endl;
}
void fun1(int i)
{
cout<<"Parent::fun1 functionality write here"<<endl;
}
void fun2()
{
cout<<"Parent::fun3 functionality write here"<<endl;
}
};
class Child:public Parent
{
public:
virtual void fun(int i)
{
cout<<"Child::fun partial functionality write here"<<endl;
Parent::fun(++i);
Parent::fun2();
}
void fun1(int i)
{
cout<<"Child::fun1 partial functionality write here"<<endl;
Parent::fun1(++i);
}
};
int main()
{
Child d1;
d1.fun(1);
d1.fun1(2);
return 0;
}
Output:
$ g++ base_function_call_from_derived.cpp
$ ./a.out
Child::fun partial functionality write here
Parent::fun functionality write here
Parent::fun3 functionality write here
Child::fun1 partial functionality write here
Parent::fun1 functionality write here
Call the parent method with the parent scope resolution operator.
Parent::method()
class Primate {
public:
void whatAmI(){
cout << "I am of Primate order";
}
};
class Human : public Primate{
public:
void whatAmI(){
cout << "I am of Human species";
}
void whatIsMyOrder(){
Primate::whatAmI(); // <-- SCOPE RESOLUTION OPERATOR
}
};
struct a{
int x;
struct son{
a* _parent;
void test(){
_parent->x=1; //success
}
}_son;
}_a;
int main(){
_a._son._parent=&_a;
_a._son.test();
}
Reference example.

How to use base class functions in C++ [duplicate]

How do I call the parent function from a derived class using C++? For example, I have a class called parent, and a class called child which is derived from parent. Within
each class there is a print function. In the definition of the child's print function I would like to make a call to the parents print function. How would I go about doing this?
I'll take the risk of stating the obvious: You call the function, if it's defined in the base class it's automatically available in the derived class (unless it's private).
If there is a function with the same signature in the derived class you can disambiguate it by adding the base class's name followed by two colons base_class::foo(...). You should note that unlike Java and C#, C++ does not have a keyword for "the base class" (super or base) since C++ supports multiple inheritance which may lead to ambiguity.
class left {
public:
void foo();
};
class right {
public:
void foo();
};
class bottom : public left, public right {
public:
void foo()
{
//base::foo();// ambiguous
left::foo();
right::foo();
// and when foo() is not called for 'this':
bottom b;
b.left::foo(); // calls b.foo() from 'left'
b.right::foo(); // call b.foo() from 'right'
}
};
Incidentally, you can't derive directly from the same class twice since there will be no way to refer to one of the base classes over the other.
class bottom : public left, public left { // Illegal
};
Given a parent class named Parent and a child class named Child, you can do something like this:
class Parent {
public:
virtual void print(int x);
};
class Child : public Parent {
void print(int x) override;
};
void Parent::print(int x) {
// some default behavior
}
void Child::print(int x) {
// use Parent's print method; implicitly passes 'this' to Parent::print
Parent::print(x);
}
Note that Parent is the class's actual name and not a keyword.
If your base class is called Base, and your function is called FooBar() you can call it directly using Base::FooBar()
void Base::FooBar()
{
printf("in Base\n");
}
void ChildOfBase::FooBar()
{
Base::FooBar();
}
In MSVC there is a Microsoft specific keyword for that: __super
MSDN:
Allows you to explicitly state that you are calling a base-class implementation for a function that you are overriding.
// deriv_super.cpp
// compile with: /c
struct B1 {
void mf(int) {}
};
struct B2 {
void mf(short) {}
void mf(char) {}
};
struct D : B1, B2 {
void mf(short) {
__super::mf(1); // Calls B1::mf(int)
__super::mf('s'); // Calls B2::mf(char)
}
};
If access modifier of base class member function is protected OR public, you can do call member function of base class from derived class.
Call to the base class non-virtual and virtual member function from derived member function can be made.
Please refer the program.
#include<iostream>
using namespace std;
class Parent
{
protected:
virtual void fun(int i)
{
cout<<"Parent::fun functionality write here"<<endl;
}
void fun1(int i)
{
cout<<"Parent::fun1 functionality write here"<<endl;
}
void fun2()
{
cout<<"Parent::fun3 functionality write here"<<endl;
}
};
class Child:public Parent
{
public:
virtual void fun(int i)
{
cout<<"Child::fun partial functionality write here"<<endl;
Parent::fun(++i);
Parent::fun2();
}
void fun1(int i)
{
cout<<"Child::fun1 partial functionality write here"<<endl;
Parent::fun1(++i);
}
};
int main()
{
Child d1;
d1.fun(1);
d1.fun1(2);
return 0;
}
Output:
$ g++ base_function_call_from_derived.cpp
$ ./a.out
Child::fun partial functionality write here
Parent::fun functionality write here
Parent::fun3 functionality write here
Child::fun1 partial functionality write here
Parent::fun1 functionality write here
Call the parent method with the parent scope resolution operator.
Parent::method()
class Primate {
public:
void whatAmI(){
cout << "I am of Primate order";
}
};
class Human : public Primate{
public:
void whatAmI(){
cout << "I am of Human species";
}
void whatIsMyOrder(){
Primate::whatAmI(); // <-- SCOPE RESOLUTION OPERATOR
}
};
struct a{
int x;
struct son{
a* _parent;
void test(){
_parent->x=1; //success
}
}_son;
}_a;
int main(){
_a._son._parent=&_a;
_a._son.test();
}
Reference example.

C++ "overload virtual warning" when implementing interface

Here is my example code:
class Interface {
public:
virtual void foo(const Interface &interface) = 0;
};
class A : public Interface {
public:
void foo(const A &a) {
// do something with another object of same type
}
};
class B : public Interface {
public:
void foo(const B &b) {
}
};
There is a warning:
warning: 'A::foo' hides overloaded virtual function
[-Woverloaded-virtual]
void foo(const A &a) {
^
note: hidden overloaded virtual function 'Interface::foo'
declared here: type mismatch at 1st parameter ('const Interface &' vs
'const A &')
virtual void foo(const Interface &interface) = 0;
How to deal with the problem? Is adding using Interface::foo; in derived class the best solution? I think it's a common problem. Thanks a lot!
void foo(const A &a) does not override void foo(const Interface &interface). While the language allows covariant return types, you can't have covariant parameters as far as I know.
So your void foo(const A &a) is hiding (or at most overloading) the base version of the function.
If you use C++11's override keyword, the compiler should more clearly tell you that you aren't overriding. Or if you try to instantiate an A then it should fail with an error about A being abstract because it hasn't actually overridden foo.
The solution of course is to make the derived versions use the exact same parameter types for foo.
The virtual part of your virtual void foo() function indicates that it can be overridden in a class that inherits Interface. In class A and B, you have used the same function but changed the parameters, which means that the function will be overloaded.
If you want to just override the function in the sub classes, use void foo(const Interface &interface).
For more details on overloading virtual functions see this post: Overloading a virtual function in a child class
Thanks #GargAnkit in comment,Here is my total solotion:
#include <iostream>
#include <string>
using std::cout;
using std::endl;
class Interface {
public:
int compare(const Interface &that) const {
if (this->to_string() < that.to_string()) {
return -1;
} else if (this->to_string() == that.to_string()) {
return 0;
} else {
return 1;
}
}
virtual std::string to_string() const = 0;
};
class A : public Interface {
public:
std::string to_string() const override {
return "A";
}
};
class B : public Interface {
public:
std::string to_string() const override {
return "B";
}
};
int main() {
A a;
B b;
cout << a.compare(b) << endl;
cout << "ok" << endl;
return 0;
}
For a function to be picked as an override of a function in the base class, both function must match exactly - same name, same parameters, same const/volatile qualification. If the function only differ by the qualification, they will be seen as totally non correlated functions, and the base function will not consider as overriden.
example::
**
class A
{
public:
virtual void fun(..)const{}
};
class B:public A
{
public:
void fun(..){} //violation
};
**

Using Templates to resolve virtual methods

This issue involves using templates to resolve virtual members in a Dispatch pattern.
Note: This is not the same as virtual template method questions already asked on StackOverflow. *
Edit 1: Corrected syntax errors, added clarifications.
Given the following:
#include <string>
#include <iostream>
class Field_Interface
{
public:
virtual std::string get_field_name(void) const = 0;
};
class Field_Integer : public Field_Interface
{
public:
std::string get_field_name(void) const
{ return "INT";}
};
class Field_String : public Field_Interface
{
public:
std::string get_field_name(void) const
{ return "VARCHAR";}
};
class Field_Double : public Field_Interface
{
public:
std::string get_field_name(void) const
{ return "DOUBLE";}
};
class Abstract_Visitor
{
public:
virtual void visit(const Field_Integer& fi) = 0;
virtual void visit(const Field_String& fi) = 0;
virtual void visit(const Field_Double& fi) = 0;
};
class Visitor_Name_Query_1 : public Abstract_Visitor
{
public:
template <class Field>
void visit(const Field& f)
{
std::cout << "Field name is: "
<< f.get_field_name()
<< "\n";
}
};
class Visitor_Name_Query_2 : public Abstract_Visitor
{
public:
void visit(const Field_Integer& fi)
{ print_field_name(fi); }
void visit(const Field_String& fi)
{ print_field_name(fi); }
void visit(const Field_Double& fi)
{ print_field_name(fi); }
private:
void print_field_name(const Field_Interface& fi)
{
std::cout << "Field name is: "
<< fi.get_field_name()
<< "\n";
}
};
int main(void)
{
Visitor_Name_Query_1 q1;
Field_Integer fi;
q1.visit(f1);
return 0;
}
The compiler is saying the templated method in Visitor_Name_Query_1 is not resolving the abstract interface from Abstract_Visitor.
Edit 2: Results from g++
# g++ -o main.exe main.cpp
main.cpp: In function `int main()':
main.cpp:75: error: cannot declare variable `q1' to be of type `Visitor_Name_Query_1'
main.cpp:75: error: because the following virtual functions are abstract:
main.cpp:35: error: virtual void Abstract_Visitor::visit(const Field_Integer&)
main.cpp:36: error: virtual void Abstract_Visitor::visit(const Field_String&)
main.cpp:37: error: virtual void Abstract_Visitor::visit(const Field_Double&)
main.cpp:77: error: `f1' undeclared (first use this function)
main.cpp:77: error: (Each undeclared identifier is reported only once for each function it appears in.)
Visitor_Name_Query_1 is an attempt to simplify the class Visitor_Name_Query_2. When the number of visit methods grows beyond a simple quantity (like 5), maintenance becomes tedious. This is the reason for the template declaration.
When the template is expanded, with one of the field types, the declaration matches the one in Visitor_Name_Query_2.
So why is the compiler generating saying that class Visitor_Name_Query_1 is abstract?
Note: I am using Visual Studio 2008 on Windows Vista.
* The other posts involve using templates to create virtual method declarations. I'm using templates to create functions that implement the abstract methods.
So why is the compiler generating saying that class Visitor_Name_Query_1 is abstract?
Because the standard says so. §14.5.2 [temp.mem]/p4:
A specialization of a member function template does not override a
virtual function from a base class. [ Example:
class B {
virtual void f(int);
};
class D : public B {
template <class T> void f(T); // does not override B::f(int)
void f(int i) { f<>(i); } // overriding function that calls
// the template instantiation
};
—end example ]
It seems you really want the Abstract_Visitor to have a default implementation. If you move the template into the Abstract_Visitor, you can let each virtual visitor have the default implementation.
class Abstract_Visitor
{
template <class Field>
void visit(const Field& f)
{
std::cout << "Field name is: "
<< f.get_field_name()
<< "\n";
}
public:
virtual void visit(const Field_Integer& fi) { visit<>(fi); }
virtual void visit(const Field_String& fi) { visit<>(fi); }
virtual void visit(const Field_Double& fi) { visit<>(fi); }
};
As all field types have a common interface, you could simplify the problem by changing the interface if it is possible:
class Abstract_Visitor
{
public:
virtual void visit(const Field_Interface& f) = 0;
};
class Visitor_Name_Query_3 : public Abstract_Visitor
{
public:
void visit(const Field_Interface& f)
{
std::cout << "Field name is: "
<< f.get_field_name()
<< "\n";
}
};

Can two classes access each other?

If I have two classes called A and B,
Note: The following doesn't compile.
class A
{
public:
static void funcA() {}
void call_funcB() { B::funcB(); } // call class B's function
};
class B
{
public:
static void funcB() {}
void call_funcA() { A::funcA(); } // call class A's function
};
Errors:
error C2653: 'B' : is not a class or namespace name
error C3861: 'funcB': identifier not found
Can you call the static functions of each class?
You have to do this:
class A
{
public:
static void funcA() {}
void call_funcB() ;
};
class B
{
public:
static void funcB() {}
void call_funcA() { A::funcA(); } // call class A's function
};
void A::call_funcB() { B::funcB(); } // call class B's function
This allows A::call_funcB() to see the B declaration.
You need to give the compiler a tip that the other class with be defined because it's a circular dependency.
class B;
class A { ... };
class A; // assuming separate file
class B { ... };
You could make funcA and funcB Friend methods.