Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with a basic program

Status
Not open for further replies.

pcjunkey

Technical User
Nov 29, 2004
17
0
0
US
Hey guys I'm learning C++, from a C++ book and its come to the chapter "Sharing with Header Files....

I have two files...

p111.cpp which looks like the following
Code:
#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
#include <string>
#include "safestuff.h"


int main(int argc, char* argv[])
{
	string SafeCracker(int SafeID);
	cout << "Surprise, surprise!" << endl;
	cout << "The Combination is (once again) " << endl;
	cout << SafeCracker(12) << endl;
	return 0;
}

and a header file safestuff.h

Code:
#include <string>

string SafeCracker(int SafeID) {
	return "13-26-16";
}

And I'm getting the following errors compiling...

--------------------Configuration: p111 - Win32 Debug--------------------
Compiling...
p111.cpp
c:\p111\safestuff.h(3) : error C2146: syntax error : missing ';' before identifier 'SafeCracker'
c:\p111\safestuff.h(3) : error C2501: 'string' : missing storage-class or type specifiers
c:\p111\safestuff.h(3) : fatal error C1004: unexpected end of file found
Error executing cl.exe.

p111.obj - 3 error(s), 0 warning(s)


Can anyone help me out?
 
You need to have the line

Code:
#include <string>

at the top of your safestuff.h file

"If it could have gone wrong earlier and it didn't, it ultimately would have been beneficial for it to have." : Murphy's Ultimate Corollary
 
snuv, if you refer to the code for my "safestuff.h" file, you'll note that it does have that line in it.
 
oops you're right
sorry about that

Are you sure that you havent got another copy of the file somewhere with that line missing?

"If it could have gone wrong earlier and it didn't, it ultimately would have been beneficial for it to have." : Murphy's Ultimate Corollary
 
Include using namespace std; or use std::string. This library namespace contains all library header names.
Apropos, use <cstdlib> C++ Standard header instead of old <stdlib.h> et al...
 
Add this after your include lines:
Code:
using std::string;
using std::cout;
using std::endl;
Also, I don't see anything in your code that uses <stdlib.h> so you can delete that.
But as ArkM said, if you do need to use it in the future you should use <cstdlib> instead of <stdlib.h> which is the old C version.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top