Tuesday, 15 February 2011

c - Programming assignment says test program will only #include .H file, not .CC file. Can someone clarify -


So I have two files for which I need to turn, call them: example.cc and example.h < / P>

We have been told that their test code will only contain "example.h". I usually include the example # example "example.cc" because it will already include example example.h is.

When I run the test code, I just get "an unspecified reference for example: Example ..", whenever I mention the example in the test code, it is clearly because the test The code can only see my delcarations in example.h, not their actual implementation.

What am I missing? I have included example.cc in Example #, but this is clearly not correct.

It appears that your professor has some other type of file, called Professor_man.cc , And they will compile this project like this:

  g.+ example.cc professor_main.cc -o project   

in their professor_main.cc One line will be

  #include "example.h"   

and also use some of your functions, and potential tests that they are doing the right thing Are:

  std :: cout & lt; & Lt; (Example_add (2,3) == 5)? "Good": "Bad" & lt; & Lt; Std :: endl;   

Now, if they do not include #home.h, or if you have not created that file correctly, they will get a compilation error at professor_main.cc, Because it is not about example_add (int, int), the important line header should be in the file and it removes this error:

  int example_add (int a, int b);   

This is called Function Declaration.

If everything has been done properly, but your professor has made a mistake and tried to compile the project without an example. In this way:

  G ++ Professor_man.cc-o project   

Then they should have an "undefined reference" about a linker < / Em> error will be found "example_add ().

What is missing definition :

  int example_add (int a, int b) { Return a + b;}   

This should be in your example.cc file, and if the professor also includes that on their compilation command line, everyone should be well.

However, if you enter # example "example.cc" in your header file Effectively, you then copy and pasted example example.cc in professor_main.cc, because it's #included in but it's clearly still in your file, example.cc so if Your professor tried to do this:

  g ++ example.cc professor_main.cc -o project   

make them a several definitions < Error from linker / em>, because function example_add () is defined by twice << p> Generally, then, what you probably should do in your source file (.cc) Definitions and all your functions in your header file (.h) announcements someone other. Try compiling it with a cc file and use it to see how it works.

No comments:

Post a Comment