| firedraco (4982) | |
| No, it's a friend function (somewhere up there in the class definition). | |
|
|
|
| kempofighter (1120) | |||
This compiled and linked for me. The friend declaration was wrong and I used consistent typenames throughout the operator<< declarations.
| |||
|
|
|||
| kakashi316 (156) | |||
did you put all of this in one file,because i need to have them seperately. | |||
|
Last edited on
|
|||
| kakashi316 (156) | |||||||
|
Im getting no more compling errors,but now im having linking errors. example [Linker error] undefined reference to `Stack<int, 100>::Push(int)' [Linker error] undefined reference to `operator<<(std::ostream&, Stack<int, 100> const&)' header:
implementation
main:
any help is appreciated | |||||||
|
|
|||||||
| kakashi316 (156) | |
| would it be something wrong with my friend class function or something else? | |
|
|
|
| jsmith (5804) | |
|
Implementations of template methods/classes must be in the header file, not a separate .cpp file. | |
|
|
|
| kakashi316 (156) | |
| unfortunately i need it in the cpp file.any suggestions? | |
|
|
|
| jsmith (5804) | |
|
Why do you need it in the .cpp file? Although it is possible, typically it is not done that way. To do it, in the .cpp file you need to explicitly instantiate the template on all types that your program will use. | |
|
|
|
| kakashi316 (156) | |||||||
|
im trying to do that,unfortunately im getting errors. 42 E:\373\S_Stack.cpp missing `>' to terminate the template argument list 42 E:\373\S_Stack.cpp template argument 2 is invalid 42 E:\373\S_Stack.cpp ISO C++ forbids declaration of `StackSize' with no type 42 E:\373\S_Stack.cpp explicit instantiation of non-template `int StackSize' 42 E:\373\S_Stack.cpp expected `;' before '>' token 43 E:\373\S_Stack.cpp missing `>' to terminate the template argument list 43 E:\373\S_Stack.cpp template argument 2 is invalid 43 E:\373\S_Stack.cpp expected `,' or `...' before '>' token 43 E:\373\S_Stack.cpp ISO C++ forbids declaration of `StackSize' with no type 43 E:\373\S_Stack.cpp template-id `operator<< <>' for `std::ostream& operator<<(std::ostream&, int)' does not match any template declaration header:
implementation:
main:
i know this method works because ive used it for other programs.Its just the second variable is whats throwing me.please,i really need help with this as soon as possible. | |||||||
|
|
|||||||
| jsmith (5804) | |||||
This isn't enough:
First of all, it's a syntax error, but secondly, even if you correct the syntax error (a bit of work), it's still a template, just only on the size. You quite literally have to do this:
etc. | |||||
|
|
|||||
| kbw (4342) | |||
It works.
| |||
|
|
|||
| kakashi316 (156) | |||||||
|
ok,got it mostly done just this last group of errors In instantiation of `std::ostream& operator<<(std::ostream&, const Stack<t, StackSize>&) [with t = int, StackItem = int, int StackSize = 100]': 43 E:\373\S_Stack.cpp instantiated from here 43 E:\373\S_Stack.cpp explicit instantiation of `std::ostream& operator<<(std::ostream&, const Stack<t, StackSize>&) [with t = int, StackItem = int, int StackSize = 100]' but no definition available 43 E:\373\S_Stack.cpp In instantiation of `std::ostream& operator<<(std::ostream&, const Stack<t, StackSize>&) [with t = int, StackItem = int, int StackSize = 100]': 43 E:\373\S_Stack.cpp instantiated from here 43 E:\373\S_Stack.cpp explicit instantiation of `std::ostream& operator<<(std::ostream&, const Stack<t, StackSize>&) [with t = int, StackItem = int, int StackSize = 100]' but no definition available 43 E:\373\S_Stack.cpp instantiated from here 43 E:\373\S_Stack.cpp explicit instantiation of `std::ostream& operator<<(std::ostream&, const Stack<t, StackSize>&) [with t = int, StackItem = int, int StackSize = 100]' but no definition available header:
implementation
main:
any help is appreciated. | |||||||
|
|
|||||||
| kbw (4342) | |
| I've posted code that works, commenting out the problem code. Why aren't you using it? | |
|
|
|
| kakashi316 (156) | |
|
cause i get linker errors [Linker error] undefined reference to `std::ostream& operator<< <int>(std::ostream&, Stack<int, 100> const&)' and i need it in more than one file. | |
|
Last edited on
|
|
| kakashi316 (156) | |||||
|
heres what i have implementation:
header:
| |||||
|
|
|||||
| kbw (4342) | |
| What compiler did you use? I reformatted, compiled and ran it thru a debugger. I think you've wasted my time. | |
|
|
|
| kakashi316 (156) | |
| dev | |
|
|
|