简体   繁体   English

C ++链接器错误

[英]C++ linker error

I"m having some problem with the following program. The program implements a stack using a linked list. I'm not showing all my code here because the code is fine. But the problem I'm having is with linking different files together. 我的以下程序有问题。该程序使用链表实现了堆栈。由于代码没问题,这里没有显示我的所有代码。但是我遇到的问题是将不同的文件链接在一起。

I'm using an IDE to run the program. 我正在使用IDE运行该程序。 When I run the TestIntStacks.cpp , the main method is supposed to call test() from StackFunctions.cpp . 当我运行TestIntStacks.cpp ,主要方法应该是从StackFunctions.cpp调用test() The test function (defined in StackFunctions.cpp ), uses the TestStack class methods. 测试函数(在StackFunctions.cpp定义)使用TestStack类方法。 Currently I'm receiving an error saying "linker error, push/pop not defined". 目前,我收到一条错误消息:“链接器错误,未定义推送/弹出”。 What I'm doing wrong? 我做错了什么? I'm sure it's something to do with a 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;
}

You are defining push() and pop( ) methods in your header file TestStack.h, but you've not provided implementations for them in TestStack.cpp. 您正在头文件TestStack.h中定义push()pop( )方法,但尚未在TestStack.cpp中为其提供实现。 You need to add the code that does the push and pop operations on your object. 您需要添加对对象执行推入和弹出操作的代码。

This error seems pretty clear to me. 这个错误对我来说似乎很清楚。 You declared push() and pop() in your header file, but the linker could not find where these methods are implemented. 您在头文件中声明了push()pop() ,但是链接器找不到在何处实现这些方法。

Where are they defined? 它们在哪里定义?

I think it has to do with the arguments provided to linker. 我认为这与提供给链接器的参数有关。 For example, a similar error occurs when you use Visual C++ 6 in a following way. 例如,当您通过以下方式使用Visual C ++ 6时,会发生类似的错误。 Let's say you created .cpp and .h files for a class. 假设您为一个类创建了.cpp和.h文件。 If you do not include cpp file into your project you get the similar error. 如果您没有在项目中包含cpp文件,则会出现类似的错误。 Because the IDE does not determine the source file based on the provided header file. 因为IDE不会根据提供的头文件确定源文件。 I don't know about dev-c++ IDE, but the solution might be similar. 我不了解dev-c ++ IDE,但解决方案可能相似。 The problem is you compile (or not) TestStack.cpp and the output of this compiling is not provided to the linker, so the linker can't find the implementation. 问题是您编译(或不编译)TestStack.cpp,并且此编译的输出未提供给链接器,因此链接器找不到实现。

You need to force the build script to use both cpp files. 您需要强制构建脚本同时使用两个cpp文件。 If you wrote your own make file, you need to build intermediate objects for each source, and then link at the end. 如果编写了自己的make文件,则需要为每个源构建中间对象,然后在最后链接。

I suspect DEV-C++ doesnt automatically generate object files or try to link everything together. 我怀疑DEV-C ++不会自动生成目标文件或尝试将所有内容链接在一起。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM