Author Topic: codeblocks has spasim with template class  (Read 3906 times)

Offline ispex

  • Single posting newcomer
  • *
  • Posts: 3
codeblocks has spasim with template class
« on: January 10, 2016, 08:34:53 pm »
Hello,
I am developing a storage template using codeblocks and the project refuses to compile with a linker error 1. However it compiles alright from the command line (in debian).

It seems I cannot implement the constructor and destructor for the class as it does not recognise them.

Here's the storage file with CNode defined, it trips up with the definition of the constructor/destructor, it seems the IDE thinks there's two constructors and two destructors.
Code
#include <stdlib.h>
#include "errors.h"

#ifndef STORAGE_H_INCLUDED
#define STORAGE_H_INCLUDED

namespace Storage {

    using namespace Errors;
    using namespace std;

    template <class Type>
    class CNode{
    public:
        CNode(Type TheData);
        ~CNode();
        Type ReturnData();
    private:
        Type *ReturnPointerToData();
        Type *m_ptrData;
    };

    template <class Type>
    CNode<Type>::CNode(Type TheData)
    {
        // attempt to allocate memory for node
        m_ptrData=(Type *) malloc(sizeof(TheData));

        // if successfull
        if (m_ptrData)
        {
            *m_ptrData=TheData; // assign what data pointer points at to data value
        }
        else
        {
            throw Errors::errrOutOfMemory; // report out of memory
        }
    }


    // destructor
    template <class Type>
    CNode<Type>::~CNode()
    {
        free((void* ) m_ptrData);
        m_ptrData=NULL;
    }

    // return data pointed to by data pointer
    template <class Type>
    Type CNode<Type>::ReturnData()
    {
        return *ReturnPointerToData();
    }

    // return POINTER to dataa
    template <class Type>
    Type *CNode<Type>::ReturnPointerToData()
    {
        return m_ptrData;
    }

}
#endif // STORAGE_H_INCLUDED

As I can successfully compile this from the command line with g++ I believe this is a codeblocks error.

I am using svn build rev 10637 (2015-12-29 16:30:44) gcc 4.9.2 Linux/unicode - 64 bit.

Thanking you in advance,
Jonathan.

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7252
Re: codeblocks has spasim with template class
« Reply #1 on: January 10, 2016, 10:03:48 pm »
As I can successfully compile this from the command line with g++ I believe this is a codeblocks error.

Most likely not C::B's fault (it's ot a compiler "just" an IDE), but an incorrect configuration.
Can you post the commandline that works on console and the content of the "Build log" (not "Build messages") of a full rebuild (clean and build) with full commandline logging ?

Offline ispex

  • Single posting newcomer
  • *
  • Posts: 3
Re: codeblocks has spasim with template class
« Reply #2 on: January 18, 2016, 06:21:42 pm »
I must apologise for this posting as I found out I was mishandling some exceptions in my code. I re-wrote it and now it compiles with code::blocks without a problem.

I should've kept the faulty code considering it did compile on debian with
Code
g++  main.cpp
. But I made one of the biggest mistakes a coder can do..... I deleted it.

My apologies,
Jonathan.