C ++ Error overriding message functions

I use two stacks to implement a queue class. My header file looks like this:

#ifndef _MyQueue_h
#define _MyQueue_h
using namespace std;

template <typename T>
class MyQueue {

public:
    MyQueue();
    ~MyQueue();
    void enqueue(T element);
    T peek();
    void dequeue();
    int size();
    bool empty();

private:
    int count;
    stack<T> stk1;
    stack<T> stk2;
};
# include "MyQueue.cpp"
# endif

And my cpp file (implementation) looks like this:

#include <stack>
#include "MyQueue.h"
using namespace std;

template <typename T>
MyQueue<T>::MyQueue()
{
    count = 0;
}

template <typename T>
MyQueue<T>::~ MyQueue()
{
}

template <typename T>
void MyQueue<T>::enqueue(T element)
{
    stk1.push(element);
    count ++;
}

(other functions omitted).

However, using Xcode 4.5, he continues to say that my functions (MyQueue, ~ MyQueue, enqueue, peek, etc.) are overridden. Can someone help me clarify where I redefined them?

thanks

+4
source share
3 answers

You're trying something that I really don't like. This is pretense.

Delete #include "MyQueue.cpp", replace it with the contents of MyQueue.cpp, delete the file MyQueue.cpp. Now everything will work.

, . , , . , , , , .

, , , cpp, , cpp. , cpp .

+5

, cpp cpp .h, .h .cpp. cpp " " .

, .

  • .cpp .h. , , , , , . ( ++!)

  • , , 'private', .cpp. .h .cpp.

  • , , , . #include "MyQueue.cpp" .h . , . , .cpp, undefined reference to MyQueue<string> :: MyQueue(). ( string , . , template MyQueue<string>; , ( MyQueue.cpp). , , , , , .

+1

when you include something, it replaces the included file with the code inside, so when you call #include "MyQueue.cpp" it replaces it with the cpp file, then your cpp file overrides it. Get rid of the line, fix it.

0
source

All Articles