0

driver.cc

#include <iostream>

#include "dynStack.h"

using namespace std;

// class definition
int main()
{
  dynstack<double> it;
  cout << "hello";
  return 0;
}

dynStack.h

template <class T>
class dynstack {
public:
  dynstack();
  void push(T data);
private:


};

#include "dynStack.cc"

dynStack.cc

template <class T>
dynstack<T>::dynstack() { // <-- Here

}

template <class T> //  // <-- And here
void dynstack<T>::push(T data)
{
}

I'm new to C++. The bolded lines are reported errors. The first one says "error: 'dynStack' does not name a type" The second one says "exrror: expected initializer before '<' token". I have spent hours on this and can't find the errors. Can anyone help? Thank you.

I was given a sample solution similar to this. Here is the sample:

main.cc

#include <iostream>
// #include the header file - as always
#include "temp.h"

using namespace std;

int main()
{
  Thing<int> it(1);

  Thing<double> dt(3.14);

  cout << endl;
  cout << "it = " << it.getData() << endl;
  cout << endl;
  cout << "dt = " << dt.getData() << endl;
  cout << endl;

  return 0;
}

temp.h

template <class T>
class Thing
{
  private:
    T data;
    void setData(T data);

  public:
    Thing(T data);
    T getData() const;
};

// MUST #include the implementation file here
#include "temp.cc"

temp.cc

// DO NOT #include the header file

template <class T>
Thing<T>::Thing(T data)
{
  this->setData(data);
}

template <class T>
void Thing<T>::setData(T data)
{
  this->data = data;
}

template <class T>
T Thing<T>::getData() const
{
  return this->data;
}
An Overflowed Stack
  • 334
  • 1
  • 5
  • 20
  • possible duplicate of [Why can templates only be implemented in the header file?](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – David G May 08 '14 at 00:45
  • 2
    When you say "dynStacl" do you mean "dynstack"? Also, is there any reason you use different casing for your file name than your class name? – derpface May 08 '14 at 00:56
  • I typed it wrong. Sorry – An Overflowed Stack May 08 '14 at 00:57
  • 2
    `#include "temp.cc"` in a header file? Seriously? From a computer science instructor? Wow. In almost every professional development organization, that would fail code review massively. – David Hammen May 08 '14 at 00:59
  • 2
    @DavidHammen, It's somewhat common in template headers, albeit with a better name/extension for the implementation. – chris May 08 '14 at 01:00
  • to Uberwulu. I do realize the casing difference, but I dont think it matters in this case. I use all lower case in the content. The only upper case is the file's name. – An Overflowed Stack May 08 '14 at 01:03
  • @chris - What's shown is not an implementation. An implementation would comprise a template specialization for a specific type, and that is something that can go in an implementation file. – David Hammen May 08 '14 at 01:07
  • 1
    @DavidHammen, Yes, it could, but what's the big problem with splitting up a long implementation from the interface with a different file when it's done often enough to be instantly recognized as such? libstdc++ has been doing this for quite some time. Aside from that, is this a common definition of implementation? It's a specialized implementation, sure, but a general one still makes sense as an implementation to me. – chris May 08 '14 at 01:21
  • @DavidHammen: Yeah normally we'd call those .hpp files. – Zan Lynx May 08 '14 at 02:07

2 Answers2

3

It appears that you are trying to compile both driver.cc and dynStack.cc. The only file you compile with this setup is driver.cc.

David Hammen
  • 32,454
  • 9
  • 60
  • 108
  • @An Overflowed Stack: Codeblocks is an IDE. It's not a compiler. It USES a compiler. What this person is suggesting is that you're trying to compile two files when there is only one you should be compiling. Check your settings to make sure the dynstack.cc is not being compiled. – derpface May 08 '14 at 01:25
1

Try this: Move contents of dynstack.cc entirely to dynstack.h and get rid of dynstack.cc

EDIT after reading comment responses:

If you want to keep dynstack.cc, its fine, just make sure you do not attempt to compile dynstack.cc I would name it a some other extension other than .cc which is conventionally for C++ implementation. Avoid .cc, .cpp, .cxx etc; use a uncommon extension such as .hc :-)

Arun
  • 19,750
  • 10
  • 51
  • 60
  • Sorry. I can't. I know your way will work. But this is my homework assignment and the professor ask us to do this. – An Overflowed Stack May 08 '14 at 00:48
  • and he gave us a sample solution similar to this. That one works, mine doesn't. I followed exact what was given. But mine throw errors, the one given works. – An Overflowed Stack May 08 '14 at 00:49
  • This is a common enough thing to do. It's just usually a more informative extension or name than *.cc. Updated, I can completely agree with the note on not compiling it. – chris May 08 '14 at 00:51
  • 1
    "I would name it a some other extension other than .cc which is conventionally for C++ implementation." Yes, like .h for example. – Jesse Rusak May 08 '14 at 00:53
  • 1
    GCC uses .tcc for these. I've been using
    _impl.hh more recently.
    – chris May 08 '14 at 00:55
  • 2
    @AnOverflowedStack: The problems you describe can be attributed to accidentally compiling the dynStack.cc in your code, but not compiling temp.cc in your teacher's sample. There really isn't any other explanation. – Mooing Duck May 08 '14 at 01:24