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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a constructor with a string parameter 1

Status
Not open for further replies.

fatlardo

Programmer
May 7, 2007
41
GB
I am new to C++ and I think I may have thrown myself in a bit too deep!! I have programmed before in Delphi and Java so I think this may be more of a syntaxical problem.

I am trying to create a class and, on calling the constructor, pass a filename to the constructor. As it stands I'm trying to use a string variable.

The problem I get is that I get errors in my header and source file. In the header file, I get the error field 'string' has incomplete type. In my source file I have the error expected ')' before 'filename'.

The code I am using is below. Anyone got any ideas of the problem?

I'm using Eclipse if that helps anyone!

Bitmap.h

#ifndef BITMAP_H_
#define BITMAP_H_

namespace gary_media
{

class Bitmap
{
public:
Bitmap(string);
virtual ~Bitmap();
private:
int * image;
int imgHeight, imgWidth;
};

}

#endif /*BITMAP_H_*/

Bitmap.cpp

#include "Bitmap.h"
#include <cstdio>
#include <string>

//using namespace gary_file;

namespace gary_media
{

Bitmap::Bitmap(string filename)
{
if (filename == "") {
return;
}

if (!FileExists(filename)) {
return;
}

FILE * pFile;
pFile = fopen ("/home/gary/Desktop/test.txt", "r");
}

Bitmap::~Bitmap()
{
}

}
 
Put the
Code:
#include<string>
into the .h file instead.



And refer to it winth the std namespace.
And make it a const ref.

Ie
Code:
#include<string>

  .
  .
  .

class Bitmap
{
public:
    Bitmap(const std::string&);
  .
  . 
  .


/Per
[sub]
www.perfnurt.se[/sub]
 
Thanks a lot. It works now :D

You'll probably hear from me again before the end of the week!!

Thanks

Gary
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top