C ++ compilation: what I did wrong

I am new to C ++ and I started my internship project where I use the snap library from stanford ( http://snap.stanford.edu/ ). So I downloaded the library, and now I'm trying to create my own little program using it. Unfortunately, I can not 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 that my compilation problems come from Snap itself, and it can be very good, only because of my poor knowledge of C ++ and how such included functions work.

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 would be very happy if someone could solve my problems (and if you want some C ++ / programming wisdom in this process, I would be even happier :))

Ortholle

+4
source share
2 answers

Snap library headers contain failed macros:

 #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 specifying STL headers before Snap, or perhaps by adding

 #undef min #undef max 

after the inclusion of the header Snap.h.

+11
source

Another problem with your code: what about all these extraneous #includes? Example: your test.cpp # includes a whole bunch of things that it doesn't need. All that test.cpp requires should (or should be) simulation.h. Model.cpp has a similar problem with too many #includes.

Do not #include something in a file that is not used in this file.

(In addition to this: random_shuffle in the simulation.cpp file should be std :: random_shuffle).

None of these fixes will help with the underlying problem, which is that the Snap library conveniently defines max and min as macros. You do not need these, so cancel them.

+1
source

All Articles