Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
What I want to do now is to make changements in FUNC.cpp and FUNC.h to inherit it to main.cpp and then, generate a diagram class at the end in which is really states that FUNC is inherited.
I want to make changements in my code, from namespace to classes, to allow the inheritance process.
I ' m having what is follow:
In a FUNC.h:
namespace FUNC
{
void f1(...);
void f2(...);
}
In a FUNC.cpp
namespace FUNC
{
void f1(...)
{
}
void f2(...)
{
}
}
in test.cpp (which is meantime a class an having its test.h) , it's possible to call f1 and f2 as follow:
FUNC::f1(...);
FUNC::f2(...);
If you change the namespace to a class, but still want to call func1 and func2 using the same syntax (e.g. FUNC::func1()), you have to make the functions static:
struct FUNC
{
static void func1();
static void func2();
};
If you want to override func1 in an inherited class then it's simple:
struct FUNC2 : public FUNC
{
static void func1();
};
There are however problems with static member functions, in that they can't access non-static members easily.
I will answer my own question
I had to insert after
class FUNC: public TEST
{
static void func1();
static void func2();
};
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have static member function inside my class which I would like to add in my namespace.
class A{
public:
static void func();
};
namespace myNamespace{
void A::func(){
...
}
}
int main(){
myNamespace::A::func;
}
I get the following errors:
error: definition of ‘void A::func()’ is not in namespace enclosing ‘A’ [-fpermissive]
error: ‘myNamespace::A’ has not been declared
myNamespace::A::func();
void A::func(){
what is the reason?
because you put a function that belongs to the class A inside namespace. just make the class and function A together inside namespace your code should be like this
namespace myNamespace {
class A {
public:
static void func();
};
void A::func() {
}
}
int main()
{
myNamespace::A::func;
return 0;
}
The declaration and the definition both should be inside the NameSpace for it to work. Here in your code, you have declared the class outside the namespace. You can fix it as follows.
namespace myNamespace{
class A{
public:
static void func();
};
void A::func(){
...
}
}
int main(){
myNamespace::A::func;
}
Also try to understand the error messages which would save you a lot of time googling.
https://www.crashhandler.com/ ---> Best Practices in C++ {EDIT :: A blog by myself}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Edit 2:
Ok so i might rephrase the question, is there a differense between an empty extending class and its base class, apart from beeing a different type? Like is the memory-size the same, and are they functionally equivalent?
Is there any difference in the compiled code and/or performance of this completely empty class definition versus an alias? Or does this depend on the compiler, in which case, it it likely to be optimised away?
class MyClass : MyTemplateClass<int>{};
using MyClass = MyTemplateClass<int>;
I want to use the class definition so I can forward declare it and avoid a circular dependency more easily.
The circular dependency:
Master.h
class Master {
void run();
State s;
}
State.h
class State {
void Modify(MyClass&);
}
MyTemplate.h
template<typename T>
class MyTemplateClass<int> {
void run(Master* pMaster) {
pMaster->run();
}
};
using MyClass = MyTemplateClass<int>;
"State.h" needs "MyTemplate.h" to be included, "Template.h" needs "Master.h" to be included, and "Master.h" needs "State.h" to be included, completing the circle. If MyClass was not an alias but an actual class, State would not need to include the template and can forward declare MyClass.
Let's see:
#include <iostream>
#include <typeinfo>
template<class T> struct MyTemplateClass {};
class MyClass1 : MyTemplateClass<int>{};
using MyClass2 = MyTemplateClass<int>;
int main()
{
std::cout << typeid(MyClass1).name() << std::endl;
std::cout << typeid(MyClass2).name() << std::endl;
}
example output:
8MyClass1
15MyTemplateClassIiE
Different types.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm writing in C++ and my code is getting bigger and bigger. I wonder if I could somehow move code fragment to a different file. Let's say i have:
class First {
class Second
{
void a();
void b();
};
void c();
};
I would like to move my Second class to a Second.h file and then of course write bodies of Second class and functions into Second.cpp file. How can I do this?
Given that class Second is nested in class First you could start by refactoring your code like so:
class First
{
class Second;
void c();
};
class First::Second
{
void a();
void b();
};
From there it is easy to break the two classes into separate files:
First.h:
class First
{
class Second;
void c();
};
And Second.h
#include "First.h"
class First::Second
{
void a();
void b();
};
And then your .cpp files would be:
First.cpp
#include "First.h"
void First::c()
{
}
Second.cpp
#include "Second.h"
void First::Second::a()
{
}
void First::Second::b()
{
}
Lets assume you have First.h
class First{
class Second
{
void a();
void b();
};
void c();
};
you can provide the implementation of class Second in a separate translation unit:
like
First.cpp:
#include "First.h"
void First::c() {
}
Second.cpp:
#include "First.h"
void First::Second::a() {
}
void First::Second::b() {
}
I'm not sure that's what you're after though.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I know that templates must be declared and defined in the same file. But, why I cannot:
#ifndef guard
#define guard
template<typename T>
class Test{
void method(){
}
};
#endif
And it is causes compiler error ( not directly but instatantiation template Test in two different place- for example in main() and as field in any class causes error.
It must be defined outside class ( It doesn't cause error like here)
#ifndef guard
#define guard
template<typename T>
class Test{
void method();
};
#endif
template<typename T>
void Test<T>::method(){}
Why?
Member functions of class templates can be defined inline, inside the class template declaration.
The code in the question:
template<typename T>
class Test{
void method(){
}
};
is well formed. Asking why you can't do it is misguided.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
#include <iostream>
class Abc // created class with name Abc
{
void display()//member function
{
cout<<"Displaying NO."<<endl;
}
};
int main()
{
Abc obj;//creating object of the class
obj.display();//calling member function inside class
}
It returns error as
main.cpp: In function 'int main()':
main.cpp:5:10: error: 'void Abc::display()' is private
void display()
^
main.cpp:13:17: error: within this context
obj.display();
^
I have tried to make display function public int main but then it gives error as
main.cpp:5:11: error: expected ':' before 'void'
public void display()
^
Declare it as:
class Abc
{
public:
void display()
{
cout<<"Displaying NO."<<endl;
}
};
or:
struct Abc
{
void display()
{
cout<<"Displaying NO."<<endl;
}
};
struct's default protection is public, class's default protection is private
The error message is clear enough. You may not call a private member function outside the class. By default classes with keyword class have private access control by default. Either write
class Abc // created class with name Abc
{
public:
void display()//member function
{
cout<<"Displaying NO."<<endl;
}
};
or
struct Abc // created class with name Abc
{
void display()//member function
{
cout<<"Displaying NO."<<endl;
}
};
Classes with keyword struct have public access control by default.
Also it would be better to define the member function as
void display() const//member function
{
std::cout<<"Displaying NO."<<endl;
}
Take into account that as you did not use directibe
using namespace std;
you have to use qualified names with entities declared in std::. For example
std::cout<<"Displaying NO."<<std::endl;