I have a problem with adding my library to MQL4.
// VectorMQL.h
#pragma once
#include <vector>
using namespace std;
template <typename T>
class Vector : public vector<T>
{
};
VectorMQL.def
LIBRARY VectorMQL
VERSION 1.00
EXPORTS
Vector DATA
VectorMQL.mqh
#import "VectorMQL.dll"
template <typename T>
class Vector : public vector<T>{};
#import
Code for Expert:
Vector <int> M;
Waiting for your reply.
Related
I'm trying to implement a Template while separating the Header and Implementation file.
I'm getting this error when building the project:
error C2955: 'Series': use of class template requires template
argument list
Header.h
#ifndef SERIES_H
#define SERIES
template <class T>
class Series {
private:
T var;
public:
Series(T v);
};
#endif
Implementation.cpp
#include <iostream>
#include "Header.h"
template <class T>
Series::Series(T v) {
var = v;
std::cout << var;
}
Main.cpp
#include <iostream>
#include "Header.h"
int main() {
Series<int> w(10);
}
The project builds successfully when everything is in one file
What do I need to do to make this work?
Newbie to C++ so please forgive me. I'm trying to write a simple template class to implement the Stack data structure functionality. I've read through this question to understand how template declaration and implementation works: Why can templates only be implemented in the header file?. But I would like to use the solution where class declaration and implementation are separate (not defined inline in .h).
MyStack.h:
#include "StackNode.h"
#include "MyStack.tpp"
template <typename T>
class MyStack
{
private:
StackNode<T> *top;
public:
MyStack();
virtual ~MyStack();
};
MyStack.tpp:
#include "MyStack.h"
template <typename T>
MyStack<T>::MyStack()
: top(nullptr)
{
}
template <typename T>
MyStack<T>::~MyStack()
{
}
main.cpp:
#include "MyStack.h"
int main() {
MyStack<int> myStack;
return 0;
}
However compiling the above gives this error:
../src/MyStack.tpp:11:1: error: 'MyStack' does not name a type
../src/MyStack.tpp:18:1: error: 'MyStack' does not name a type
I know I'm missing something, but don't understand what. Thanks!
The problem is that you are including the .tpp file, which contains the class method definitions, before the class has actually been declared yet. You need to move that #include statement below the class declaration instead.
Also, the .tpp file should not be #include'ing the .h file that originally #include'd it. In fact, that won't work correctly since your .h file is missing header guards to prevent multiple inclusions within the same translation unit (ie, main.cpp in this case). So, you would end up with MyStack.h which includes MyStack.tpp which includes MyStack.h again, which causes an error when it tries to re-declare things that are already declared. Always declare a header guard in your .h files.
Try this instead:
MyStack.h:
#ifndef MyStack_H
#define MyStack_H
#include "StackNode.h"
template <typename T>
class MyStack
{
private:
StackNode<T> *top;
public:
MyStack();
virtual ~MyStack();
};
#include "MyStack.tpp"
#endif
MyStack.tpp:
template <typename T>
MyStack<T>::MyStack()
: top(nullptr)
{
}
template <typename T>
MyStack<T>::~MyStack()
{
}
I am trying to create a vector class that looks something like this:
template <typename T>
class Vector
{
.
.
.
};
#include "vector.cpp"
However, when I start writing my functions in "vector.cpp", CLion complains that I have duplicate functions. How do I work around this? I believe in NetBeans, I can add vector.h & vector.cpp to a folder called "Important Files" which would fix the problem. I am not sure what the equivalent in CLion is.
General Design of a template
example.h
#ifndef EXAMPLE_H
#define EXAMPLE_H
// Needed includes if any
// Prototypes or Class Declarations if any
template<class T> // or template<typename T>
class Example {
private:
T item_;
public:
explicit Example( T item );
~Example();
T getItem() const;
};
#include "Example.inl"
#endif // EXAMPLE_H
Example.inl
// Class Constructors & Member Function implementations
template<class T>
Example<T>::Example( T item ) : item_(item) {
}
template<class T>
Example<T>::~Example() {
}
template<class T>
T Example<T>::getItem() const {
return item_;
}
Example.cpp
#include "Example.h"
// Only need to have the include here unless if
// the class is non template or stand alone functions
// are non-template type. Then they would go here.
I have a variadic template class and I'm trying to include my namespace which is in another file so that I can use a few functions from it in the class.
However, the file including the namespace also includes a using statement related to the class as I need the variadic class for a few functions in that namespace as well.
I have many errors trying to do this and was wondering if there is a way to achieve my goal...
Here is what I mean:
#include "VariadicClass.hpp"
#include <stdint.h>
using VariadicClass3 = VariadicClass<3, int>
namespace mynamespace {
int function1(VariadicClass3 param){return 1;}
int function2(){return 0;}
}
////////////////////////////////////////////
#include "MyNamespace.hpp"
#include <stdint.h>
template<std::size_t T_size, typename T>
class VariadicClass3 {
public:
//...
void function1(){
auto some_var = mynamespace::function2();
}
};
You have couple of typos in your posted code. Ignoring that for the time being, you can use forward declaration as shown below to remove the cyclic dependency:
MyNamespace.hpp:
#pragma once
#include <cstdint>
// Forward declare the class.
template<std::size_t T_size, typename T> class VariadicClass;
using VariadicClass3 = VariadicClass<3, int>;
namespace mynamespace {
// Use the class only in reference in declaration.
// If the implementation of the function needs to
// access to members of VariadicClass3, it needs to be
// in a .cpp file where VariadicClass.hpp can be #include'd.
int function1(VariadicClass3 const& param){return 0;}
int function2(){return 1;}
}
VariadicClass.hpp:
#pragma once
#include "MyNamespace.hpp"
template<std::size_t T_size, typename T>
class VariadicClass {
public:
//...
void function1(){
auto some_var = mynamespace::function2();
}
};
I'm currently working on a template graph class that utilizes two vectors to create an adjacency matrix. I can get this to work outside of the template class, but I can't seem to initialize the vectors.
Here is what I have:
#include <stdexcept>
#include <vector>
#include <list>
#include <string>
using namespace std;
namespace GraphNameSpace
{
template <class T>
class Graph
{
private:
vector<int> colOfRow(100);
vector<vector<int> > matrix(100);
};
}
I receive:
error: expected identifier before numeric constant
error: expected ‘,’ or ‘...’ before numeric constant
What is the reason that those won't initialize in the template class, and what would a solution to this be?
I know this may not be the most efficient way of doing it, but it's the way I understand best. If you would consider another way to be better, would you provide the way you would go about doing this?
It has nothing to do with template class. You could use member initializer list to initialize member variables:
namespace GraphNameSpace
{
template <class T>
class Graph
{
private:
vector<int> colOfRow;
vector<vector<int> > matrix;
public:
Graph() : colOfRow(100), matrix(100) {}
};
}
Or default member initializer (since c++11):
namespace GraphNameSpace
{
template <class T>
class Graph
{
private:
vector<int> colOfRow{100};
vector<vector<int> > matrix{100};
};
}