Why templates must be defined outside class [closed] - c++

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.

Related

How I can implement the constructor of the class who is inline explicite? [closed]

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
How I can implement the constructor of this class? The constructor is inline and explicit and call the draw function. I try to implement that's in the file .h and .cpp and that's generate an error.
#ifndef DRAW_H
#define DRAW_H
#include "item.h"
#include <set>
#include "random/random.hpp"
namespace nvs
namespace lotto
{
class Parameter;
class Draw : public Item
{
inline std::set<unsigned> draw(const Parameter & parameter) const;
public:
/*!
* this function call the private function draw()
*/
inline explicit Draw(const Parameter & parameter);
/*!
* \brief constructor virtuel by default.
*/
virtual ~Draw() override = default;
};
inline explicit Draw::Draw(const Parameter & parameter)
{
draw(parameter);
}
inline std::set<unsigned> Draw::draw(const Parameter & parameter) const
{
std::set<unsigned> values;
unsigned cpt=0;
unsigned length = parameter.length();
while (cpt < length)
{
unsigned temp = random_value(parameter.maximum(), parameter.minimum());
if ((values.find(temp))==values.end())
{
values.insert(temp);
cpt++;
}
}
return values;
}
} // namespace lotto
} // namespace nvs
#endif
#endif // DRAW_H
Output :
C:\Users\George\Documents\TD07\draw.h:86: erreur : 'explicit' outside class declaration
inline explicit Draw::Draw(const Parameter & parameter)
^
C:\Users\Mitch\Documents\TD07\draw.h:86: erreur : no matching function for call to 'nvs::lotto::Item::Item()'
You can't have the explicit keyword outside the declaration in the class. If you add it to the declaration, it is marked explicit and you don't need to add it to the definition. Just remove it from the definition and it should be solved. That is exactly what the error is saying.
On that note, you have the inline keyword on the definition and declaration. Adding it to only one of them should be enough and the other one may be remove.

Static member functions in namesapce [closed]

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}

alias vs empty new extending class [closed]

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.

C++ template function inside a non-template class [duplicate]

This question already has answers here:
How to create a template function within a class? (C++)
(4 answers)
Closed 7 years ago.
I am looking to start putting in templates in my c++ class code but I have come across a situation I have not experienced before. Basically I have a non-templates class but only 1 function in the class I need to be templated.
class example
{
public:
example();
~example();
<template T> templatefunction(T);
nontemplatefunction(string x);
};
Is this possible? If so, is it a common solution or am I looking at templates completely in error?
As people have noted in the comments, there's no problem doing so.
One aspect to watch out for is where to put the definition of the method templatefunction. For the time being (see the ISO cpp FAQ), you should consider placing it in the header file, which is different than what you'd probably do with the definition of the other methods. Thus you'd have example.hpp:
class example
{
public:
example();
~example();
template<typename T> void templatefunction(T);
void nontemplatefunction(string x);
};
template<typename T> void example::templatefunction(T)
{
}
and then example.cpp:
example::example(){}
void example::nontemplatefunction(string x)
{
}

Inheriting classes and namespaces [closed]

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();
};