Author Topic: C++ separate file compilation confusion :/  (Read 3434 times)

0 Members and 5 Guests are viewing this topic.

pllaybuoy

  • Guest
C++ separate file compilation confusion :/
« on: October 15, 2012, 08:25:18 pm »
These two files are in the project
 
Code: [Select]
//main.cpp
#include <cstdlib>
 #include <iostream>
 using namespace std;
 #include "prots.h"
 
 
 
 
 int main(int argc, char *argv[])
 {
 player player1;
 player player2;
 cout<<"Number 1 \n";
 getinfo(player1);
 cout<<"Number 2\n";
 getinfo(player2);
 cout<<"Number 2\n";
 showinfo(player2);
 
 cout<<"Number 1\n";
 showinfo(player1);
 
 
 
 
 
 
 system("PAUSE");
 return EXIT_SUCCESS;
 }
 
AND
Code: [Select]
//fucntions.cpp
 #include "prots.h"
 
 int getinfo(player &a){
 cout<<"ABOUT PLAYER \n";
 cout<<"Name :\t";
 cin>>a.name;
 cout<<"Age :\t";
 cin>>a.age;}
 
 int showinfo(player &a){
 cout<<"PLAYER INFORMATION \n";
 cout<<"NAME :\t"<<a.name;
 cout<<"\nAGE :\t"<<a.age;}
 
and the next file is a header file , I am compiling from devcpp so I didn't add this file in the project but created it in the same folder
 
Code: [Select]
//prots.h
 #ifndef pro_ts
 #define pro_ts
 struct player{std::string name;int age;};
 
 
 
 
 void getinfo(player&a);
 void showinfo(player&a);
 
 
 #endif
 
Now on compiling it gives the following error
 "In file included from functions.cpp"
 on the first line of functions.cpp where I have included the prots.h header file , I had to include it because the functions are for structures which are defined in prots header file

Offline blackboxx

  • /dev/null
  • *
  • Posts: 11
  • Cookies: 0
  • Programmer, Student
    • View Profile
Re: C++ separate file compilation confusion :/
« Reply #1 on: October 16, 2012, 07:59:05 pm »
First, I would strongly recommend you use Codeblocks, if you are on Windows. It will allow you to create a project, containing multiple .h and .cpp files, much easier.


First, for prots.h, you needed to include #include<iostream> at the top.


Secondly, for your prots.h, you declared two "void" function prototypes, but in your implementation, in functions.cpp, you changed their data types to "int". Changing them to void, pretty much fixed compilation problems. Also, adding "using namespace std;" in functions.cpp, allows you to use "cout" and "cin".  Otherwise, use "std::cout" or "std::cin".


Here is the updated code, which should compile, in their respective files:


main.cpp, prots.h, functions.cpp, respectively.
Code: [Select]

//main.cpp
#include <cstdlib>
 #include <iostream>
 using namespace std;
 #include "prots.h"








 int main(int argc, char *argv[])
 {
 player player1;
 player player2;
 cout<<"Number 1 \n";
 getinfo(player1);
 cout<<"Number 2\n";
 getinfo(player2);
 cout<<"Number 2\n";
 showinfo(player2);


 cout<<"Number 1\n";
 showinfo(player1);












 system("PAUSE");
 return EXIT_SUCCESS;
 }


prots.h

#ifndef PROTS_H_INCLUDED
#define PROTS_H_INCLUDED


#include<iostream>  // Needed to include #include<iostream>


struct player{std::string name; int age;};


void getinfo(player&a);
void showinfo(player&a);


#endif // PROTS_H_INCLUDED


functions.cpp

//fucntions.cpp
 #include "prots.h"


using namespace std;    // To make cout, and cin work.


 void getinfo(player &a){   // Wrong data type
 cout<<"ABOUT PLAYER \n";
 cout<<"Name :\t";
 cin>>a.name;
 cout<<"Age :\t";
 cin>>a.age;}


 void showinfo(player &a){
 cout<<"PLAYER INFORMATION \n";
 cout<<"NAME :\t"<<a.name;
 cout<<"\nAGE :\t"<<a.age;}
« Last Edit: October 16, 2012, 08:07:19 pm by blackboxx »

pllaybuoy

  • Guest
Re: C++ separate file compilation confusion :/
« Reply #2 on: October 16, 2012, 08:07:32 pm »
Copy pasted the exact code you gave and still the same error
"In file included from main.cpp"

Offline blackboxx

  • /dev/null
  • *
  • Posts: 11
  • Cookies: 0
  • Programmer, Student
    • View Profile
Re: C++ separate file compilation confusion :/
« Reply #3 on: October 16, 2012, 08:16:15 pm »
Yea, you cannot copy exactly what I inputted, since there are three separate file's code included. For some reason, this forum wouldn't allow me to make three separate boxes for each file's code.


I  included attachments.
« Last Edit: October 16, 2012, 08:17:36 pm by blackboxx »

pllaybuoy

  • Guest
Re: C++ separate file compilation confusion :/
« Reply #4 on: October 16, 2012, 08:24:28 pm »
It worked thanks a tons dude !

Offline blackboxx

  • /dev/null
  • *
  • Posts: 11
  • Cookies: 0
  • Programmer, Student
    • View Profile
Re: C++ separate file compilation confusion :/
« Reply #5 on: October 16, 2012, 08:27:37 pm »
No probz.  ;)

Offline Xires

  • Noob Eater
  • Administrator
  • Knight
  • *
  • Posts: 379
  • Cookies: 149
    • View Profile
    • Feed The Trolls - Xires
Re: C++ separate file compilation confusion :/
« Reply #6 on: October 23, 2012, 03:27:09 pm »
It might be useful to look up the term 'makefile'.  This is a topic that you will probably need to know in the future as you cannot always rely upon an IDE to take care of it for you.
-Xires

pllaybuoy

  • Guest
Re: C++ separate file compilation confusion :/
« Reply #7 on: October 23, 2012, 03:39:30 pm »
It might be useful to look up the term 'makefile'.  This is a topic that you will probably need to know in the future as you cannot always rely upon an IDE to take care of it for you.
makefile is in C# not c++

Offline Satan911

  • VIP
  • Knight
  • *
  • Posts: 289
  • Cookies: 25
  • Retired god/admin
    • View Profile
Re: C++ separate file compilation confusion :/
« Reply #8 on: October 23, 2012, 03:43:36 pm »
makefile is in C# not c++

Couldn't be more wrong. Please look it up properly.
Satan911
Evilzone Network Administrator

pllaybuoy

  • Guest
Re: C++ separate file compilation confusion :/
« Reply #9 on: October 23, 2012, 03:53:53 pm »
Couldn't be more wrong. Please look it up properly.

Oops sorry , its for LINUX i believe
makefile is in linux as far as i remember

Offline Daemon

  • VIP
  • Baron
  • *
  • Posts: 845
  • Cookies: 153
  • A wise man fears a gentle mans anger
    • View Profile
Re: C++ separate file compilation confusion :/
« Reply #10 on: October 23, 2012, 06:54:14 pm »
Lol pllaybuoy, you obv haven't paid enough attention to the programming boards. Xires is never wrong, like I have yet to see him post something that was false. And you'll also notice he really doesn't participate in other boards, just ones on code really. So if Xires says something and you think he's wrong or telling you something that won't help, then you better checkity check yourself before you wreckity wreck hourself :P

As for makefile, yes its linux. Are you not on linux? If not, then makefile is yet another reason to get on *nix bro.

Now someone correct me if I'm wrong, but you don't need need prots.h in order to make structs. I have several code examples I just pulled up and no prots.h included...where are you getting your info from OP?
This lifestyle is strictly DIY or GTFO - lucid

Because sexploits are for h0edays - noncetonic


Xires burns the souls of HF skids as a power supply

pllaybuoy

  • Guest
Re: C++ separate file compilation confusion :/
« Reply #11 on: October 23, 2012, 08:28:06 pm »
Nah dude I wasn't trying to mock on Xires  and yes I know I am stupid :S btw no, I am not into linux these days , I am on windows 7 since somebody else shares my computer casually and they don't know shit about linux .
I've been using codeblocker and devcpp and sometimes visual studio so never encountered makefile in windows .
And about where I am getting info ?so different sources lol

Offline Xires

  • Noob Eater
  • Administrator
  • Knight
  • *
  • Posts: 379
  • Cookies: 149
    • View Profile
    • Feed The Trolls - Xires
Re: C++ separate file compilation confusion :/
« Reply #12 on: October 23, 2012, 09:44:07 pm »
pllaybuoy; makefiles are used for every platform.  It's more a language thing and less a platform thing.  Software like Code::Blocks tend to generate the makefiles internally.  In fact, if you look at the options in the IDE, there are features that allow you to specify or create your own makefile or even to list entries into the one that it generates.


Daemon; structs are language features that don't require headers.  Their usage is core to the language itself so they fall into the category of other special keywords like 'if', 'while', 'for', 'enum', 'typedef', etc.


Incidentally..I *have* been wrong before, I think.  So it's not a complete impossibility.  It might be somewhat unlikely, especially on the topic of C & C++, since I'm most familiar with those languages and I teach them more than any other subject, but it's certainly not impossible.
« Last Edit: October 23, 2012, 09:52:16 pm by Xires »
-Xires

pllaybuoy

  • Guest
Re: C++ separate file compilation confusion :/
« Reply #13 on: October 23, 2012, 10:06:38 pm »
pllaybuoy; makefiles are used for every platform.  It's more a language thing and less a platform thing.  Software like Code::Blocks tend to generate the makefiles internally.  In fact, if you look at the options in the IDE, there are features that allow you to specify or create your own makefile or even to list entries into the one that it generates.


Daemon; structs are language features that don't require headers.  Their usage is core to the language itself so they fall into the category of other special keywords like 'if', 'while', 'for', 'enum', 'typedef', etc.


Incidentally..I *have* been wrong before, I think.  So it's not a complete impossibility.  It might be somewhat unlikely, especially on the topic of C & C++, since I'm most familiar with those languages and I teach them more than any other subject, but it's certainly not impossible.
Okay dude , and hey I've asked a question about c++ in the c++ section about OOP , can you please answer that too?

Offline Daemon

  • VIP
  • Baron
  • *
  • Posts: 845
  • Cookies: 153
  • A wise man fears a gentle mans anger
    • View Profile
Re: C++ separate file compilation confusion :/
« Reply #14 on: October 23, 2012, 11:01:16 pm »
pllaybuoy; makefiles are used for every platform.  It's more a language thing and less a platform thing.  Software like Code::Blocks tend to generate the makefiles internally.  In fact, if you look at the options in the IDE, there are features that allow you to specify or create your own makefile or even to list entries into the one that it generates.


Daemon; structs are language features that don't require headers.  Their usage is core to the language itself so they fall into the category of other special keywords like 'if', 'while', 'for', 'enum', 'typedef', etc.


Incidentally..I *have* been wrong before, I think.  So it's not a complete impossibility.  It might be somewhat unlikely, especially on the topic of C & C++, since I'm most familiar with those languages and I teach them more than any other subject, but it's certainly not impossible.

Just like I thought, you don't need any special library for making structs, classes, or constructors. OP, you can remove that library now. Thanks for clarifying Xires, good to know I'm right once in awhile :P

And what I meant about Xires being right is that you need to double check what you think before you go calling him wrong, odds are good that he knows what he's talking about and like I said. I have yet to see him make a mistake ;)
This lifestyle is strictly DIY or GTFO - lucid

Because sexploits are for h0edays - noncetonic


Xires burns the souls of HF skids as a power supply