-1

I got two classes "DTreeEmbedder" and "modifier". Embedder is a template class and I want to manipulate member variables of "DTreeEmbedder".

class DTreeEmbedder:

class modifier; //forward declaration of modifier

using namespace ogdf;
using namespace ogdf::energybased::dtree;

template<int Dim>
class DTreeEmbedder
{
  public:
  //! constructor with a given graph, allocates memory and does 
  initialization
  explicit DTreeEmbedder(GLFWwindow * w, const Graph& graph);

  //! destructor
  virtual ~DTreeEmbedder();

  modifier* m_modifier;

In constructor

  template<int Dim>
  DTreeEmbedder<Dim>::DTreeEmbedder(GLFWwindow * w, const Graph& graph) : 
             m_graph(graph)
  {
        m_modifier = new modifier();
  }

Both objects need access to each other, therefore the forward declaration.

#pragma once

#include "DTreeEmbedder.h"

class modifier
{
  public:
    modifier(DTreeEmbedder<int>* e);
    ~modifier();

    DTreeEmbedder<int>* m_embedder;

    void pca_project(int pc1,int pc2);
 };

pca_project is a function that should change values / call functions in m_embedder

In constructor of modifier:

modifier::modifier(DTreeEmbedder<int>* e)
{
   m_embedder = e;
}

pca function:

void modifier::pca_project(int pc1, int pc2)
{
   m_embedder->stop();
}

My approach therefore is:

  1. Create DTreeEmbedder
  2. DTreeEmbedder creates modifier with pointer of itself
  3. modifier got the pointer to DTreeEmbedder and can now change values of that object

My errors are:

"int": Invalid type for the non-type template parameter "Dim"
This pointer can not be converted from "DTreeEmbedder" to "DTreeEmbedder <Dim> &"

Thx in advance

  • What do you mean by `template`? – CinCout Oct 09 '18 at 10:19
  • 4
    @CinCout it's a non-type template parameter – bolov Oct 09 '18 at 10:22
  • 3
    then it should not be `DTreeEmbedder` but rather something like `DTreeEmbedder<42>` in the modifier class – bobah Oct 09 '18 at 10:25
  • 2
    [**Do not** use `using namespace` in headers!](https://stackoverflow.com/q/5849457/1968) – Konrad Rudolph Oct 09 '18 at 10:38
  • @Konrad Rudolph DTreeEmbedder is a header only file and i need the namespace to gain access to another class – dondroelf Oct 09 '18 at 11:08
  • @bobah best case would be, if the pointer stays variable – dondroelf Oct 09 '18 at 11:10
  • @dondroelf Please read the text I linked to. You can *use* a namespace, you simply mustn’t import it via `using namespace` on the file level. – Konrad Rudolph Oct 09 '18 at 11:17
  • "template class". I suspect this is part of your problem. The usual English syntax parses that as a "a class, which is usable as a template". The problem is, there's no such thing in C++. What C++ has are **class templates**. They are templates. Because they're templates, they can be **instantiated**. And the instantiation of a class template is a class. (And by the same logic, the instantiation of a function template is a function). – MSalters Oct 09 '18 at 13:22

1 Answers1

1

Change template<int Dim> to template<class Dim>

OR

Look at the error closely, it says:-

"int": Invalid type for the non-type template parameter "Dim"

Here you have assigned a class inside a parameter that you have so declared as template<int Dim>.

What "non-type" here stands for is that you are assigning a class inside a template whose parameter is an int (NOT class).

Example:

template<int Dim>
int Sum(int with)
{
    return Dim + with;
}


Explanation:

Here, a "function template" is declared with a template parameter Dim of type int. This is the variable that will hold the integer(Not a class, a.k.a, what you are trying to do there...) that will be assigned to it when used. Like so:-

auto S = Sum<2>(2);

Here you have assigned an integer "2" in the parameters, the template takes the value of Dim (In this case, 2) and adds it with with, (2 in this case) parameter inside the function and returns the result of the expression. (2 + 2 = 4)


Where your problem lies is in this constructor,

modifier(DTreeEmbedder<int>* e)

Here, there is one problem, given that you have declared Dim there as an integer, now adding a class int inside the parameter is wrong...

So it should be:-

modifier(DTreeEmbedder<111>* e) // Or any other number, since Dim is an integer

Think of this template parameter as a real integer variable, so you are doing something like:-

int Dim = int; /*Which is a type (class)*/

in

DTreeEmbedder<int> /*Assigning a class to an integer (Dim in this case)*/

The other error is an assignment error, check if both the variables are same, even the integer values assigned inside the template have to match since Dim is an integer...

Like, DTreeEmbedder<90> is not equal to DTreeEmbedder<91> or something like that...

Kind regards,

Ruks.

Ruks
  • 3,886
  • 1
  • 10
  • 22