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

ort*_*lle 4 c++ compilation

我是c ++的新手,我已经为我的实习开始了一个项目,我从斯坦福德(http://snap.stanford.edu/)开始使用Snap库.所以我已经下载了库,我现在正在尝试使用它创建自己的小程序.可悲的是,我似乎无法编译它:(

以下是来源:

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
Run Code Online (Sandbox Code Playgroud)

simulation.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
Run Code Online (Sandbox Code Playgroud)

simulation.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;
}
Run Code Online (Sandbox Code Playgroud)

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);
}
Run Code Online (Sandbox Code Playgroud)

我不认为我的编译问题来自Snap本身,而且很可能只是因为我对c ++的不了解以及包含等等是如何工作的.

这是我在运行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
Run Code Online (Sandbox Code Playgroud)

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

Ortholle

int*_*jay 11

Snap库头包含不幸的宏定义:

#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
Run Code Online (Sandbox Code Playgroud)

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

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

#undef min
#undef max
Run Code Online (Sandbox Code Playgroud)

包含Snap.h头后.