4

I have a program were I was given the compiled .o file but I do not have the original .cc file and also I only have a half-way finished header file. The header file has all the method signatures but is lacking the private variable declarations. I am trying to get this .o file to work with the project but get a segmentation fault at the constructor of the class defined by the .o file. The program compiles. How do I get this to work? The program is a homework assignment and the teacher does not want us to see the .cc file. Also my teacher knows about the issue. I am just trying to figure it out on my own (and hopefully with the help of you guys :) ). I thought I had done this before a while ago with another teacher but did not have any problems. There is a makefile that is used to compile the program.

3
  • How are you trying to compile your project, and what errors are you getting? Commented Oct 30, 2011 at 1:35
  • 1
    Does the header lack private class members or only other stuff? Commented Oct 30, 2011 at 1:38
  • Does the class have any static methods? Could you post the header? I think this is solvable. Commented Oct 30, 2011 at 2:57

1 Answer 1

4

If you are using a C++ program, and the header file includes class definitions, the class definitions must exactly match those that were used to originally build the file. This is the One Definition Rule. If your professor has removed private variable declarations from the class definitions, you will likely end up with crashes; this is because your different .o files will disagree about the size of the objects defined by those classes.

If your professor wants to hide the implementation of the class, they should use the p/impl pattern. If you want to use the header file, you must remove the mangled class definitions entirely and not attempt to use them (you can use a forward definition as in class Foo; to satisfy any functions which take/return the class as a pointer parameter, however).

Sign up to request clarification or add additional context in comments.

3 Comments

Only able to pass the class to functions that take a pointer is rarely possible in C++ (unless you use C style classes)
@Dani, indeed, but if you don't have the real declaration that's the best you can do...
There is another way: find what size the class is by the methods disassembly, and them pad it to that size.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.