#Help! Classroom Problem: Two .cpp files, one .h file.

44 messages · Page 1 of 1 (latest)

naive tundra
#

Hello I'm having trouble with this problem given to us.

We are given three files. One .cpp file WITH an int main(), one .cpp file WITHOUT an int main(), and a .h file (I'll name the two .cpp files .cpp(1) and .cpp(2) respectively).

The .h file contains a class it declares.

The .cpp(2) file contains the functions related to this class.

The .cpp(1) file contains the int main(), #include the .h file, and has hard coded input. It does not #include the .cpp(2) file so it fails to compile.

I #include-d the .cpp(2) file and it compiles successfully however there are two rules in this homework:

  1. Keep all three files separately
  2. Do not #include implementation files (which is what I assume .cpp(2) is)

I cannot post the full code for legal reasons but I can answer any clarifications on the code/program.

(this is also a terminal based program)

Thank you!

silver ploverBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

novel cape
#

you're supposed to also compile the second .cpp file

naive tundra
#

How do I do that?

novel cape
#

like g++ one.cpp two.cpp

#

specify both files when compiling

naive tundra
#

Sorry, I have not compiled and ran manually before, I've always been using the C/C++ Compile Run extension

#

after compiling, what command do I do to run the program?

novel cape
naive tundra
#

yep

novel cape
#

it just doesn't work when compiling multiple files

naive tundra
#

ahhhhh okok

#

Sorry, what's the command for running the compiled program?

novel cape
#

./program

naive tundra
#

Thank you!

#

One more question

#

if you don't mind

novel cape
#

;compile bash cat > a.cpp << EOF int foo(){ return 42; } EOF cat > a.hpp << EOF //should include some header guards in general int foo(); EOF cat > b.cpp << EOF #include"a.hpp" #include<iostream> int main(){ std::cout<<foo()<<'\n'; } EOF g++ -oT a.cpp b.cpp ./T

plucky cedarBOT
#
Program Output
42

novel cape
#

for example

novel cape
naive tundra
#

what's the difference between gcc and g++

#

my prog prof told us to use gcc when compiling

novel cape
#

g++ links the C++ standard library and other things necessary for compiling C++ code

naive tundra
#

ohhhhh okok thank you

novel cape
#

;compile bash cat > a.cpp << EOF int foo(){ return 42; } EOF cat > a.hpp << EOF //should include some header guards in general int foo(); EOF cat > b.cpp << EOF #include"a.hpp" #include<iostream> int main(){ std::cout<<foo()<<'\n'; } EOF gcc -oT a.cpp b.cpp

plucky cedarBOT
#
Program Output
/usr/bin/ld: /tmp/ccObuGxa.o: in function ​`main':
b.cpp:(.text+0x12): undefined reference to ​`std::cout'
/usr/bin/ld: b.cpp:(.text+0x17): undefined reference to ​`std::ostream::operator<<(int)'
/usr/bin/ld: b.cpp:(.text+0x24): undefined reference t
novel cape
#

see the error here

naive tundra
#

yep I got something similar

#

thank you for all of your help!

novel cape
naive tundra
#

ahhh okok

#

I see I see

#

my prof recommend us to use CodeBlocks as our ide

#

but I'm already familiar with vsc

#

but thank you!

#

I'll marked this as solved

#

!solved

silver ploverBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

novel cape
#

;compile bash cat > a.cpp << EOF int foo(){ return 42; } EOF cat > a.hpp << EOF //should include some header guards in general int foo(); EOF cat > b.cpp << EOF #include"a.hpp" #include<iostream> int main(){ std::cout<<foo()<<'\n'; } EOF gcc -oT a.cpp b.cpp -lstdc++ ./T

plucky cedarBOT
#
Program Output
42

novel cape
#

here is an example of doing that

naive tundra
#

okok

pseudo frost