4 Replies - 1494 Views - Last Post: 12 March 2010 - 04:29 PM Rate Topic: -----

#1 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

how to compile two .cpp and a .h file?

Posted 12 March 2010 - 02:13 PM

We have been requested to break up our classes into .cpp and .h files, where .cpp is the driver and .h contains everything else, but they want a class.cpp class.h and a main.cpp...


I'm having trouble combining these to compile.

here is my code, any help is appreciated.
//main.cpp
#include <iostream>
#include <string>
#include "Encryptable.h"
using namespace std;


int main() {
	char choice = 'y';
	EncryptableString X;

	while(choice == 'y' || choice == 'Y')
	{
		X.EncString(X.returnInput());
		cout << "Would you like to do another (y/n): ";
		cin >> choice;
	}

	cout << "Thank you for using the program. Exiting now!" << endl;
	return 0;
}



//Encryptable.h

class EncryptableString
{
private:
	string userInput;
public:
	void EncString(string);		// Function to encrypt the user string
	string returnInput() ;	// reutrns the user defined string
};



//EncMain.cpp
#include <string>
#include <iostream>
#include "Encryptable.h"
using namespace std;

string EncryptableString::returnInput() {
	cin.clear();
	cin.sync();
	cout << "Please enter a string: ";
	getline(cin, userInput);
	return userInput;
}


void EncryptableString::EncString(std::string userInput)
{

		for(int i = 0; i < userInput.size(); i++) 
		{
			if(userInput[i] == 'Z')
				userInput[i] = 'A' - 1;
			if(userInput[i] == 'z')
				userInput[i] = 'a' - 1;
			if(userInput[i] == ' ')
				userInput[i] = '.';
			else
				userInput[i]++;
		}
	cout << userInput << endl;
};



Is This A Good Question/Topic? 0
  • +

Replies To: how to compile two .cpp and a .h file?

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: how to compile two .cpp and a .h file?

Posted 12 March 2010 - 02:24 PM

You need to add header guards to your header file. Also, since you use "string", and not "std::string", then add an "using namespace std;". Here's the modfied header:
//Encryptable.h
#ifndef ENCRYPTABLE_H
#define ENCRYPTABLE_H

// You could get rid of the preprocessor #ifndef-#endif 
// and use #pragma once if you use VC++ 

using namespace std;

class EncryptableString
{
private:
        string userInput;
public:
        void EncString(string);         // Function to encrypt the user string
        string returnInput() ;  // reutrns the user defined string
};

#endif


The header guards will make sure that the .h is not included multiple times.
With those modifications, I was able to compile your project :) (using MSVC 2008)

This post has been edited by sarmanu: 12 March 2010 - 02:25 PM

Was This Post Helpful? 0
  • +
  • -

#3 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: how to compile two .cpp and a .h file?

Posted 12 March 2010 - 03:39 PM

like saramanu said you need header guards, but think of it that what ever header you have declared in you header file, then it is also included in all of its cpp file.

example
//Encryptable.h
#ifndef ENCRYPTABLE_H //if ecnryptiable.h is not defined
#define ENCRYPTABLE_H //define it

#include <string>
#include <iostream>

using namespace std;


class EncryptableString
{
private:
        string userInput;
public:
        void EncString(string);         // Function to encrypt the user string
        string returnInput() ;  // reutrns the user defined string
};

#endif



now your encrypt.cpp and main.cpp file does not need the headers <string> or <iostream>
Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: how to compile two .cpp and a .h file?

Posted 12 March 2010 - 04:07 PM

View PostImaSexy, on 12 March 2010 - 05:39 PM, said:

example
//Encryptable.h
//...
#include <string>
#include <iostream>
//...



Au contraire.

It is bad practice to #include headers in other headers unless really necessary, because it means that those extra headers will be included in all code that includes the first header, whether or not they are needed, resulting in possible conflicts and making the code less re-usable than it would otherwise be. Admittedly, <iostream> is not likely cause problems, but still it is a bad habit to get into.

In this case, <string> is needed because EncryptableString instantiates a std::string, but <iostream> should be included in main.cpp and in EncMain.cpp, not in Encryptable.h.
Was This Post Helpful? 0
  • +
  • -

#5 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: how to compile two .cpp and a .h file?

Posted 12 March 2010 - 04:29 PM

Yeah you can run into problems, but it was just an example because he had #include <string> in both cpp file
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1