C++ 链接器错误

标签 c++

我在使用以下程序时遇到了一些问题。该程序使用链表实现堆栈。我没有在此处显示我的所有代码,因为代码很好。但我遇到的问题是链接不同的文件放在一起。

我正在使用 IDE 来运行程序。当我运行 TestIntStacks.cpp 时,主要方法应该从 StackFunctions.cpp 调用 test()。测试函数(在 StackFunctions.cpp 中定义)使用 TestStack 类方法。 目前我收到一条错误消息,提示“链接器错误,推送/弹出未定义”。我做错了什么?我确定这与 namespace 有关。

MyStack.h
-------------------------------------
namespace A
{
    class Node{
        public :
            char data;
            StackNode* link;
            StackNode(int v=0): data(v), link(NULL){ }
    };

    class MyStack{
        private:
            Node * top;

        public:
            MyStack():top(NULL){ }
            void push(int c);
    };
}//namespace


//TestStack.cpp
--------------------------------------------------------------
#include "MyStack.h"

namespace A
{
    void MyStack::push(int x)
    {
        StackNode *temp = new StackNode(x);
        temp->link = top;
        top = temp;
    }
}

//StackFunctions.cpp
-----------------------------------------------------------
#include <iostream>
#include "TestStack.h"

using namespace std;
using namespace A;

void test()
{
    MyStack st;
    st.push(1);
    st.push(2);
    st.push(3);
    st.push(4);
}

// TestIntStacks.cpp
----------------------------------------------------------------
// Code for testing the TestStack
// from the A namespace.

#include <iostream>
#include <vector>
using namespace std;
#include "TestStack"
#include "StackFunctions.cpp"

void test();
int main()
{
    test();
    system("pause");
    return 0;
}

最佳答案

您在头文件 TestStack.h 中定义了 push()pop() 方法,但是您没有在 TestStack.cpp 中为它们提供实现。您需要添加对对象执行推送和弹出操作的代码。

关于C++ 链接器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5045287/

相关文章:

c++ - 如何加载(mov)一个类变量到cpu寄存器

c++ - std::ios_base::sync_with_stdio 如何影响流缓冲?

c++ - QML,如何从 C++ 动态更改 Repeater 的项目

C++ 写内存函数与指针

c++ - 我正在编写一个程序来使用玩家工具来保存游戏的统计数据。我正在上课,但遇到了一些问题。我不知道如何测试它

c++ - Xcode 和 C++ header

c++ - 这个 reinterpret_cast 可以吗

c++ - const 参数传递 : invalid conversion

c++ - 在 C++ 中返回对局部变量的引用

c++ - VS C++ 程序仅在从文件夹运行 .exe 时才有效? [不是VS调试]