Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ compilation : what did i do wrong

I'm new to c++, and I've started a project for my internship where I have use to the Snap library from stanford (http://snap.stanford.edu/). So I have downloaded the library and I am now trying to create my own little programm using it. Sadly i can't seem to be able to compile it :(

Here are the sources :

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

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

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;
}

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);
}

I don't think my compilation problems come from Snap itself and it might very well be only from my poor knowledge of c++ and how the includes an so on are working.

Here is what I get after running 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

I'd be very glad if some one could help resolve my problems (and if you feel like giving some c++/programming wisdom in the process i'd be even happier :) )

Ortholle

like image 225
ortholle Avatar asked Jun 16 '11 23:06

ortholle


2 Answers

The Snap library headers contain the unfortunate macro definitions:

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

This will cause problems with code that uses (or defines) std::min and std::max.

You can get around this by making sure to include STL headers before Snap, or possibly by adding

#undef min
#undef max

after including the Snap.h header.

like image 109
interjay Avatar answered Sep 22 '22 16:09

interjay


Another problem with your code: What's with all those extraneous #includes? Example: Your test.cpp #includes a whole bunch of stuff it doesn't need. All that test.cpp needs is (or should need) simulation.h. simulation.cpp has a similar problem with far too many #includes.

Don't #include something in a file that isn't used in that file.

(Aside: that random_shuffle in simulation.cpp should be std::random_shuffle).

None of these fixes are going to help with the base problem, which is that the Snap library 'conveniently' defines max and min as macros. You don't need these, so undef them.

like image 37
David Hammen Avatar answered Sep 26 '22 16:09

David Hammen