Menu

Problems with templates!

Anonymous
2003-02-12
2012-09-26
  • Anonymous

    Anonymous - 2003-02-12

    Clearly, my ambition to learn C++ is inversely proportional to the amount of time I have to spend on solving f***ing configuration problems before I can even start with the simplest of programs. After lots of trouble I was finally able to build and run a simple "Hello World"-program. So I thought I should continue with a timple C++-template, implementing a stack. On this page:
    http://66.176.248.167:81/c_plusplus/tutorial/templates/using.aspx
    there is a tutorial about C++-templates, including a small example program that creates a stack. All I did was creating a new project, copying the code from the above URL, and placing it in two files in my project: stack.h and stackTest.cpp (both in the same directory). Now, when I try to build this project I get (of course) an error message:

    Compiler: Default compiler
    Building Makefile: "C:\Program\Programming\C++\Dev-Cpp\egna\stackTest\Makefile.win"
    Executing make...
    make.exe -f "C:\Program\Programming\C++\Dev-Cpp\egna\stackTest\Makefile.win" all
    g++.exe -c stackTest2.cpp -o stackTest2.o -I"C:/Program/Programming/C++/Dev-Cpp/include" -I"C:/Program/Programming/C++/Dev-Cpp/lib/gcc-lib/mingw32/2.95.3-6/include" -I"C:/Program/Programming/C++/Dev-Cpp/include/g++-3" -I"C:/Program/Programming/C++/Dev-Cpp/include" -I"C:/Program/Programming/C++/Dev-Cpp/include/g++-3" -I"C:/Program/Programming/C++/Dev-Cpp/egna/ADT"

    stackTest2.cpp: In function `int main(...)':
    stackTest2.cpp:6: ANSI C++ forbids declaration `Stack' with no type
    stackTest2.cpp:6: template-id `Stack<float>' used as a declarator
    stackTest2.cpp:6: parse error before `;'

    stackTest2.cpp:7: non-template type `Stack' used as a template

    stackTest2.cpp:7: ANSI C++ forbids declaration `IntStack' with no type
    stackTest2.cpp:9: `FloatStack' undeclared (first use this function)
    stackTest2.cpp:9: (Each undeclared identifier is reported only once
    stackTest2.cpp:9: for each function it appears in.)
    stackTest2.cpp:9: parse error before `('
    stackTest2.cpp:12: `fs' undeclared (first use this function)
    stackTest2.cpp:25: warning: initialization to `int' from `double'
    stackTest2.cpp:27: request for member `push' in `is', which is of non-aggregate type `int'
    stackTest2.cpp:34: request for member `pop' in `is', which is of non-aggregate type `int'

    make.exe: *** [stackTest2.o] Error 1

    Execution terminated

    If anyone knows what can be wrong, please help!

     
    • Nobody/Anonymous

      Guess you copied the wrong stack.h...
      Here it compiled with only a few glitches..
      // snip //----
      In file included from main.cpp:2:
      stack.h:2:9: warning: #pragma once is obsolete
      main.cpp:5: `main' must return `int'

      main.cpp: In function `int main(...)':
      main.cpp:25: warning: initialization to `int' from `double'

      main.cpp:25: warning: argument to `int' from `double'

      make.exe: *** [main.o] Error 1

      Execution terminated
      // snip //---

      http://66.176.248.167:81/c_plusplus/tutorial/templates/class_member.aspx

      is the url for the correct header.
      /Roger

       
    • Anonymous

      Anonymous - 2003-02-12

      The "stack.h" on the URL you mentioned is exactly the one I have in my program. Too bad it doesn't work. And even worse we get different error messages. =(

       
    • Anonymous

      Anonymous - 2003-02-13

      On that URL I mentioned in my first message there are one .cpp and one .h file. I created a new C++ project in dev and added these two files to the project, and then I get the error message you can see in my first message. I have no idea what may be wrong...

       
    • Anonymous

      Anonymous - 2003-02-13

      You are learning C++ and you went from "hello world" to templates?!!

       
    • Anonymous

      Anonymous - 2003-02-13

      Hehe...actually the "Hello World" was just to test that dev-cpp worked. I have programmed some Java before and I know the basics of C++ so I'm no total newbie to programming. When I wrote "learning C++" I meant learning it above the most basic level. And the template example I tried to compile and run is a "simple" template program (after all it is part of a tutorial on templates). However it doesn't work and I have no clue why.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.