c++编译:我做错了什么

c++ compilation : what did i do wrong

本文关键字:错了 什么 编译 c++      更新时间:2023-10-16

我是c++的新手,我已经开始了一个实习项目,在那里我可以使用来自stanford的Snap库(http://snap.stanford.edu/)。所以我已经下载了这个库,现在我正试图用它创建我自己的小程序。遗憾的是,我似乎无法编译它:(

以下是来源:

Makefile:

CXXFLAGS += -std=c++98 -Wall
LDFLAGS += -lrt
Snap.o :
    g++ -c  $(CXXFLAGS) ../snap/snap/Snap.cpp -I../snap/glib -I../snap/snap -pg
simulation.o : simulation.cpp simulation.h
            g++ -g -c $(CXXFLAGS) simulation.cpp
test.o : test.cpp
            g++ -g -c $(CXXFLAGS) test.cpp
test : test.o Snap.o simulation.o
            g++ -g $(LDFLAGS) test.o Snap.o simulation.o -I../snap/glib -I../snap/snap -lm -o test

仿真.h

#ifndef SIMULATION
#define SIMULATION
#include <vector>
#include "../snap/snap/Snap.h"
class Simulation{
    public:
        Simulation():score(-1),nNodes(-1),nEdges(-1), dMax(-1){};
        Simulation(int nN, int nE, int d);
        Simulation(int d, PUNGraph g);
        void setDMax(int d){ dMax = d; }
        double getScore(){ return score; }
        int getNNodes(){ return nNodes; }
        int getNEdges(){ return nEdges; }
        int getDMax(){ return dMax; }
        PUNGraph getGraph(){ return graph; }
        std::vector<int> getAlignment(){ return alignment; }
        double computeEnergy();
    private:
        double score;
        int nNodes;
        int nEdges;
        int dMax;
        PUNGraph graph;
        std::vector<int> alignment;
};
#endif

模拟.cpp

#include "simulation.h"
#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include "../snap/snap/Snap.h"
Simulation::Simulation(int nN, int nE, int d){
    nNodes = nNodes;
    nEdges = nEdges;
    dMax = dMax;
    graph = TSnap::GenRdnGnm<PUNGraph>(nNodes,nEdges);
    for(int i=1; i<=nNodes; i++){
        alignment.push_back(i);
    }
    random_shuffle(alignment.begin(),alignment.begin()+nNodes);
    computeEnergy();
}
Simulation::Simulation(int d, PUNGraph g){
    nNodes = graph->GetNodes();
    nEdges = graph->GetEdges();
    dMax = d;
    graph = g;
    for(int i=1; i<=nNodes; i++){
        alignment.push_back(i);
    }
    random_shuffle(alignment.begin(),alignment.begin()+nNodes);
    computeEnergy();
}
double computeEnergy(){
    return 0.0;
}

test.cpp

#include<stdlib.h>
#include<stdio.h>
#include<vector>
#include<algorithm>
#include "simulation.h"
#include "../snap/snap/Snap.h"

int main(int argc, char** argv){
    Simulation sim(5000,30000,30);
}

我不认为我的编译问题来自Snap本身,这很可能只是因为我对c++以及includes等的工作方式知之甚少。

以下是运行make后我得到的:

g++ -g -c -std=c++98 -Wall simulation.cpp
In file included from /usr/include/c++/4.5/bits/stl_algo.h:61:0,
                 from /usr/include/c++/4.5/algorithm:63,
                 from simulation.cpp:5:
/usr/include/c++/4.5/bits/algorithmfwd.h:347:41: error: macro "max" passed 3 arguments, but takes just 2
/usr/include/c++/4.5/bits/algorithmfwd.h:358:41: error: macro "min" passed 3 arguments, but takes just 2
/usr/include/c++/4.5/bits/algorithmfwd.h:343:5: error: expected unqualified-id before ‘const’
/usr/include/c++/4.5/bits/algorithmfwd.h:343:5: error: expected ‘)’ before ‘const’
/usr/include/c++/4.5/bits/algorithmfwd.h:343:5: error: expected ‘)’ before ‘const’
/usr/include/c++/4.5/bits/algorithmfwd.h:343:5: error: expected initializer before ‘const’
/usr/include/c++/4.5/bits/algorithmfwd.h:347:5: error: template declaration of ‘const _Tp& std::max’
/usr/include/c++/4.5/bits/algorithmfwd.h:354:5: error: expected unqualified-id before ‘const’
/usr/include/c++/4.5/bits/algorithmfwd.h:354:5: error: expected ‘)’ before ‘const’
/usr/include/c++/4.5/bits/algorithmfwd.h:354:5: error: expected ‘)’ before ‘const’
/usr/include/c++/4.5/bits/algorithmfwd.h:354:5: error: expected initializer before ‘const’
/usr/include/c++/4.5/bits/algorithmfwd.h:358:5: error: template declaration of ‘const _Tp& std::min’
In file included from /usr/include/c++/4.5/algorithm:63:0,
                 from simulation.cpp:5:
/usr/include/c++/4.5/bits/stl_algo.h: In function ‘void std::__merge_sort_loop(_RandomAccessIterator1, _RandomAccessIterator1, _RandomAccessIterator2, _Distance)’:
/usr/include/c++/4.5/bits/stl_algo.h:3172:26: error: expected unqualified-id before ‘(’ token
/usr/include/c++/4.5/bits/stl_algo.h: In function ‘void std::__merge_sort_loop(_RandomAccessIterator1, _RandomAccessIterator1, _RandomAccessIterator2, _Distance, _Compare)’:
/usr/include/c++/4.5/bits/stl_algo.h:3202:26: error: expected unqualified-id before ‘(’ token
simulation.cpp: In constructor ‘Simulation::Simulation(int, int, int)’:
simulation.cpp:11:13: error: ‘GenRdnGnm’ is not a member of ‘TSnap’
simulation.cpp:11:38: error: expected primary-expression before ‘>’ token
simulation.cpp:11:47: warning: left-hand operand of comma has no effect

如果有人能帮助解决我的问题,我会很高兴(如果你想在这个过程中给予一些c++/编程智慧,我会更高兴:)

正交

Snap库头包含错误的宏定义:

#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))

这将导致使用(或定义)std::minstd::max的代码出现问题。

您可以通过确保在Snap之前包含STL头,或者可能通过添加来解决这个问题

#undef min
#undef max

在包括Snap.h标头之后。

代码的另一个问题是:所有这些无关的#includes是怎么回事?示例:你的test.cpp#包含了一大堆它不需要的东西。test.cpp所需要的只是(或者应该需要)模拟。h.simulation.cpp也有类似的问题,#includes太多了。

不要在文件中#包含未在该文件中使用的内容。

(另外:simulation.cpp中的random_shuffle应该是std::random_shuffle)。

这些修复都无助于解决基本问题,即Snap库"方便地"将max和min定义为宏。你不需要这些,所以解开它们。