Good Evening (in CET) to everyone.
I am in need of a bit of help.
I have the following cpp and.h files:

dirmain.cpp

#include "dirseq.h"
#include <iostream>
#include <algorithm>
#include "dirseq.h"
#include <string>

struct IsEven
{
  bool operator()( int i ) const
  {
    return i % 2 == 0;
  }
};

const int max = 1024;

int main()
{
  int yourMark = 1;

  DirectionSequence<int> di;
  di.push_back( 1 );
  di.push_back( 2 );
  di.push_back( 5 );
  const DirectionSequence<int> cdi = di;
  di.change_direction();
  di.push_back( 3 );
  di.push_back( 7 );
  DirectionSequence<std::string> ds;
  ds.push_back( "Test" );
  DirectionSequence<double> x;
  for( int i = 0; i < max; ++i )
  {
    x.push_back( i / 10.0 );
    x.change_direction();
  }
  if( 1 == cdi.at( 0 ) &&
      5 == di.at( 0 ) &&
      100.5 < x.at( 1 ) &&
      4 == ds.at( 0 ).length() )
  {
    yourMark = di.size() - cdi.size();
  }

dirseq.h

#ifndef DIR_H
#define DIR_H

#include <iostream>
#include <string>

template <class D>
class DirectionSequence
{
    public:

    DirectionSequence();
    void push_back(const D ad);
    void change_direction();
    const D at(const int vp);
    int size();
  private:

    D v;
    int vp;
    int tn;
    int als,fels;
    D temp;

    template <class T>
   DirectionSequence()
    {
        v= new D[1024];
        vp=0;
        tn=0;

    };
    template <class T>
    void push_back(const D ad)
    {
        if (tn<1024)
        {v[vp++]= ad;
        tn++;}
        else std::cout<<"megtelt a verem.";

    }

    template <class T>
     const D at(const int vp)
    {
        return (v[vp]);
    }

    template <class T>
     int size(D ad)
    {
        return tn;
    }

    template <class T>
    void change_direction()
        {
            als=0;
            fels=tn;
            while (als<=fels)
                {
                    temp=v[als];
                    v[als]=v[fels];
                    v[fels]=temp;
                    als++;
                    fels--;
                }
        }
};
#endif

The Cpp file cannot be modified!

The errors:

g:\herpaai\dirmain.cpp|37|error: passing 'const DirectionSequence<int>' as 'this' argument of 'const D DirectionSequence<D>::at(int) [with D = int]' discards qualifiers|
g:\herpaai\dirmain.cpp|42|error: passing 'const DirectionSequence<int>' as 'this' argument of 'int DirectionSequence<D>::size() [with D = int]' discards qualifiers|

where have I gone wrong?

Thank you for the help.

Whenever an object is const, it means that it cannot be modified. Like in the main() function, the object cdi is const and cannot be changed. Now, the problem is that if you call a member function (like at()) on that object, how can the compiler make sure that the object is not modified within that function? It needs to know that the function that was called in particular will not modify the object. The way to tell that to the compiler and to enforce that immutability within the function is to append the const keyword at the end of the function declaration:

class DirectionSequence {
  //...
    int size() const;  // notice the 'const' here, 
                       // it means this function will modify the object.
  //..
};

You can do the same with the at() function, and it will fix the particular problem that you reported. However, you have plenty more problems to worry about.

I think you didn't quite understand the idea behind the public/private access rights. You declared a bunch of member functions in the public interface of the class, and then you implemented these functions in the private part. That's not what public/private means in this context. The access right specifiers (public, private, and protected) only pertain to how outside code (outside as in, code that is not part of your class' code) can access or use your class. Functions and members that are public are accessible directly from anywhere (any part of the code). Functions and members that are private are only accessible to the code that is part of the implementation of the class itself (i.e., not accessible from the outside). This has nothing to do with where you put the implementation of the member functions. As far as the compiler sees it, in your code, the functions declared in the public interface and the functions defined in the private part are two separate sets of functions. So, if I take the example of the size() function, here are your choices:

Either you define the function inside the class declaration:

template <class D>
class DirectionSequence {
  public:
    //.. other functions.
    // now, the size function:
    int size() const {
      return tn;
    };

  private:
    //.. only the private data members (no functions).
};

Or, you define the function outside the class declaration, as so:

template <class D>
class DirectionSequence {
  public:
    //.. other functions.
    // now, the size function declaration only:
    int size() const;
  private:
    //.. only the private data members (no functions).
};

// then, the implementation:
template <class D>
int DirectionSequence<D>::size() const {
  return tn;
};

And, btw, in your code, the variables als, fels, and temp are only used in the function called change_direction(). You should declare them inside that function implementation, as local variables of the function, they don't need to be data members of the class.

Comments
full-on, understandable explenation

Thank you for the answer, but now there is another problem. I am getting 'Undefined reference' for every function. I know that is a linking problem, but as far as I can see, in main I do

#include "dirseq.h"

so i really don't see why it shouldn't be working.

Edited 4 Years Ago by Psyho

The undefined reference means that none of your functions actually have an implementation for them. And as per the issue that I explained (the two choices of where to put your function implementations), if you follow that, it should work. As I said, with the code you posted before, none of your public functions have a corresponding implementation, and thus, all the undefined reference errors.

Thank you for the answer, but now there is another problem. I am getting 'Undefined reference' for every function. I know that is a linking problem, but as far as I can see, in main I do

#include "dirseq.h"

so i really don't see why it shouldn't be working.

to learn template compilation. It says that putting everything in the header can bloat your ex

Thank you mike and triumphost for your help. Everything is working now, and I hope I gained a better understanding thanks to both of you.

Best Regards,

Another question rose up:
The changedirection() function works, but here:

  const DirectionSequence<int> cdi = di;
  di.change_direction();

cdi changes direction too, and it isn't supposed to. Why?

This question has already been answered. Start a new discussion instead.