-1

I have 1 header file and 2 .cpp files (I've included main.cpp) and work on CodeBlocks. My problem is that compiler not see the errors in the second .cpp file (graph_interface.cpp) when I build it. Do you have any suggestions? Where am I going wrong?.

Simply I get the output even I did the wrong thing in graph_implementation.cpp file:

 Target is up to date.
 Nothing to be done (all items are up-to-date).

For example, my code in main.cpp

//main.cpp
#include "graph_interface.h"

int main(int argc,char *argv[])
   {
     return 0;
   }

graph_interface.h file

  #ifndef GRAPH_INTERFACE_H_INCLUDED
  #define GRAPH_INTERFACE_H_INCLUDED

  typedef struct{int v;int w;} Edge;
  Edge EDGE(int,int);

  typedef struct graph *Graph;
  Graph GRAPHinit(int);
  void GRAPHinsertE(Graph,Edge);
  void GRAPHremoveE(Graph,Edge);
  int GRAPHedges(Edge[],Graph G);
  Graph GRAPHcopy(Graph);
  void GRAPHdestroy(Graph);
  #endif // GRAPH_INTERFACE_H_INCLUDED

And other cpp file graph_implementation.cpp

 #include "graph_interface.h"
 using namespace std;
 struct graph{
   int V;
   int E;
   int **adj;
 };
 Graph GRAPHinit(int V){
 Graph G=malloc(sizeof *G);
    G->V=V;
    G->E=0;
    G->adj=MATRIXint(V,V,0);
    return G;
    ;// not give anything
 }
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
emre
  • 1
  • 1
  • 2
    Not sure if it's a typo or your problem but the "other cpp file" is missing the ending `"` on the include statement. – DigitalNinja Mar 24 '15 at 00:56
  • Your {}'s don't match up. – David Schwartz Mar 24 '15 at 00:57
  • Writing C++ with `malloc()` calls is not a good idea. Use `new` and `delete`, and maybe `vector<>` too. – Jonathan Leffler Mar 24 '15 at 01:05
  • Your description describes `graph_interface.h` as a `.cpp` file; presumably, that is a typo for `graph_interface.cpp`? I didn't correct it because it might be the cause of your trouble; you're compiling just the header and not the source (`.cpp`) file. – Jonathan Leffler Mar 24 '15 at 01:10
  • @JonathanLeffler Yes for first question. Is that a problem when i compile it ? in main.cpp file not empty on my code i did not add it here. But even that it againg not give any error – emre Mar 24 '15 at 01:24
  • I added #include "graph_interface.cpp" in main.cpp and it works fine now. Thank you for your attention – emre Mar 24 '15 at 01:48
  • @JonathanLeffler actually i have little question. you said "using malloc() calls is not a good idea" may use the new as graph* G=new graph(); is the be true or not? thanks – emre Mar 24 '15 at 02:02
  • Apart from needing to use `Graph` rather than `graph`, yes — that was what I had in mind. – Jonathan Leffler Mar 24 '15 at 02:17
  • I don't know Code::Blocks at all, but it looks like it runs `make` on your behalf, and it looks like `make` doesn't know it needs to compile `graph_interface.o`, so maybe you didn't tell Code::Blocks to compile `graph_interface.cpp` yet? Just a guess... – Jonathan Leffler Mar 24 '15 at 02:24
  • @JonathanLeffler i changed as Graph G=new graph(); actuall I was so confused if it correct or not – emre Mar 24 '15 at 02:26
  • This is probably a build system problem rather than a programming problem. – bames53 Mar 24 '15 at 02:26
  • Note that you should not `#include "filename.cpp"` under normal circumstances. You should compile the file separately, and link the object files together. Or build the object file into a library and then link with the library. You can — as you've just demonstrated – do `#include "filename.cpp"` and it does work. But it is not a good idea. – Jonathan Leffler Mar 24 '15 at 02:26
  • I think you should be using `Graph *G = new Graph();`. – Jonathan Leffler Mar 24 '15 at 02:27
  • i tried Graph *G = new Graph(); before but it not work – emre Mar 24 '15 at 02:30
  • I'm assuming `Graph` is a typedef for `graph *`. Note that malloc is called with `sizeof *G`, which would be `sizeof(graph)`. – bames53 Mar 24 '15 at 02:32
  • @bames53 yes - in graph_interface.h used typedef struct graph *Graph; and Graph G = new graph(); is fine but is that correct? in the future any anamoly come from here? – emre Mar 24 '15 at 02:36
  • @emre Using 'naked new' or malloc like that is against modern practice because it's very difficult to do correctly. This style is classic C resource allocation though. – bames53 Mar 24 '15 at 02:42
  • I beg your pardon; please see [Is it a good idea to typedef pointers?](http://stackoverflow.com/questions/750178/is-it-a-good-idea-to-typedef-pointers) (to which the answer is 'No') for why I misled myself. Why not stick to C++ conventions instead of trying to import C conventions to C++? In C++, you can use `class Graph { ... };` and thereafter, `Graph` is a type — without a need for an explicit `typedef`. And you can replace `class` with `struct` and the only difference is that the members of a `struct` are public by default, whereas the members of a class are private by defatult. – Jonathan Leffler Mar 24 '15 at 03:05
  • Given the fact that `Graph` is a `graph *`, then `Graph G = new graph();` should work correctly — but will leave people horribly confused. Go with the normal naming conventions. If you want the type to be `Graph`, use `class Graph` or `struct Graph`; if you want the type to be `graph`, use `class graph` or `struct graph`. Leave out the `typedef` altogether; it is C-speak (and I speak as someone who is primarily a C programmer and only partly a C++ programmer). – Jonathan Leffler Mar 24 '15 at 03:08

1 Answers1

0

If you're expecting the line:

;// not give anything

to produce an error, the reason you aren't seeing one is because empty statements are perfectly legal in C++; This line does not contain any error.

// complete, legal C++ program
int main() {
  ;
}

Using new/delete or malloc/free this way goes against modern C++ practice because it's very difficult to do correctly. Modern practice would be more like:

class Graph {
  int V;
  Matrix adj;
public:
  Graph(int V)
    : V(V), adj(V*V, 0)
  {}
}

int main() {
  Graph g; // automatically initialized and destroyed correctly

  // ...
}
bames53
  • 86,085
  • 15
  • 179
  • 244