我不知道为什么会发生此错误
我很困惑。

错误1错误LNK2019:未解析的外部符号“ public:__thiscall Graph :: Leingth :: Leingth(void)”(?? 0Leingth @ Graph @@ QAE @ XZ)在函数“ public:__thiscall Graph :: queMember :: queMember( int,int,struct Graph :: Leingth)“(?? 0queMember @ Graph @@ QAE @ HHULeingth @ 1 @@ Z)C:\ Users \ kevin \ Documents \ Visual Studio 2013 \ Projects \ Project7 \ Project7 \ Source.obj项目7
错误2错误LNK1120:1未解析的外部C:\ Users \ kevin \ Documents \ Visual Studio 2013 \ Projects \ Project7 \ Debug \ Project7.exe 1 1 Project7

主要

#include <iostream>
#include <fstream>
#include "Graph.h"
#include <string>

using namespace std;

int main()
{
    Graph g(ifstream("Text.txt"));
    LCAPath p = g.ShortestCommonAncester(3, 5);
    cout << p.Leingth << endl << p.LeastCommonAncester << endl << p.path;
}


Graph程序

#pragma once
#include <iostream>
#include <fstream>

using namespace std;

struct LCAPath
{
    int LeastCommonAncester;
    int Leingth;
    string path = "";
};
class Graph
{
public:
    struct PathGroupMember
    {
        int Id = NULL; //THE NODE POINTED TO
        PathGroupMember * p = NULL;
        void add(int nextID)
        {
            if (Id = NULL)
            {
                Id = nextID;
            }
            else if (p = NULL)
            {
                p = new PathGroupMember();
                p->Id = nextID;
            }
            else p->add(nextID);
        }
    };
    struct Node
    {
        PathGroupMember * PathGroup = new PathGroupMember;
    };
    Graph(ifstream FStream)
    {
        if (FStream) FStream >> nodes;
        if (FStream) FStream >> paths;
        NodeList = new Node[nodes];
        for (int i = 0; i < paths; i++)
        {
            if (FStream)
            {
                int temp1;
                int temp2;
                FStream >> temp1;
                if (FStream)
                {
                    FStream >> temp2;
                    NodeList[temp1].PathGroup->add(temp2);
                }
            }
        }
    }
    LCAPath ShortestCommonAncester(int a, int b)
    {
        int* aa = new int[1];
        int* bb = new int[1];
        aa[0] = a;
        bb[0] = b;
        return ShortestCommonAncestor(aa, 1, bb, 1);
    }
    struct Leingth
    {
        Leingth();
        Leingth(int a, int b, string aa, string bb)
        {
            FromA = a;
            FromB = b;
            PathA = aa;
            PathB = bb;
        }
        int FromA = -1;
        int FromB = -1;
        string PathA = "";
        string PathB = "";
    };
    struct queMember
    {
        int Fromnode;
        int Tonode;
        Leingth l;

        queMember(int From, int To, Leingth L)
        {
            Fromnode = From;
            Tonode = To;
            l = Leingth(L);
        }

        queMember * next = NULL;
    };
    struct que
    {
        queMember * Front;
        queMember * Back;
        void add(queMember* M)
        {
            if (Front == NULL) Front = M;
            Back->next = M;
            Back = M;
        }
        queMember* remove()
        {
            if (Front = NULL) return NULL;
            queMember * temp = Front;
            Front = Front->next;
            return temp;
        }
        bool hasNext()
        {
            if (Front == NULL)return false; return true;
        }
    };
    LCAPath ShortestCommonAncestor(int* a, int GroupASize, int*b, int GroupBSize)
    {

        Leingth* BestLeingthList = new Leingth[nodes];
        que q;
        for (int i = 0; i < GroupASize; i++)
        {
            for (PathGroupMember* p = NodeList[a[i]].PathGroup; p != NULL; p = p->p)
            {
                queMember* t = new queMember(i, p->Id, Leingth(1,0,(i +"-" + p->Id),""));
                q.add(t);
            }
        }

        while (q.hasNext())
        {
            queMember * t = q.remove();
            if (t->l.FromA < BestLeingthList[t->Tonode].FromA)
            {
                BestLeingthList[t->Tonode] = t->l;
                for (PathGroupMember* p = NodeList[t->Tonode].PathGroup; p != NULL; p = p->p)
                {
                    string path = t->l.PathA + "-";
                    path += p->Id;
                    queMember* temp = new queMember(t->Fromnode, p->Id, Leingth(t->l.FromA + 1, 0, path, ""));
                    q.add(temp);
                }
            }
        }

        for (int i = 0; i < GroupBSize; i++)
        {
            for (PathGroupMember* p = NodeList[b[i]].PathGroup; p != NULL; p = p->p)
            {
                queMember* t = new queMember(i, p->Id, Leingth(0, 1, "", (p->Id + "-" + i)));
                q.add(t);
            }
        }
        int best = -1;
        while (q.hasNext())
        {
            queMember * t = q.remove();
            if (t->l.FromB < BestLeingthList[t->Tonode].FromB)
            {
                BestLeingthList[t->Tonode].FromB = t->l.FromB;
                BestLeingthList[t->Tonode].PathB = t->l.PathB;


                if (BestLeingthList[t->Tonode].FromB + BestLeingthList[t->Tonode].FromA < BestLeingthList[best].FromB + BestLeingthList[best].FromA || (best = -1))
                    best = t->Tonode;
                for (PathGroupMember* p = NodeList[t->Tonode].PathGroup; p != NULL; p = p->p)
                {
                    string path = p->Id + " " + t->l.PathB;
                    queMember* temp = new queMember(t->Fromnode, p->Id, Leingth(0, t->l.FromB + 1, "", path));
                    q.add(temp);
                }
            }
        }
        LCAPath returnval;
        returnval.LeastCommonAncester = best;
        returnval.Leingth = BestLeingthList[best].FromA + BestLeingthList[best].FromB;
        returnval.path = BestLeingthList[best].PathA + BestLeingthList[best].PathB;
        return returnval;
    }
private:
    int nodes;
    int paths;
    Node * NodeList;
};

最佳答案

您已声明Graph::Leingth的默认构造函数,但尚未在任何地方定义它。

换线

    Leingth();




    Leingth() {}


那应该解决它。

PS:您的意思是使用拼写错误的Leingth而不是拼写正确的Length吗?

关于c++ - 我无法追踪的错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34170746/

10-11 22:36
查看更多