×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Creating a constructor with a string parameter

Creating a constructor with a string parameter

Creating a constructor with a string parameter

(OP)
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()
{
}

}

RE: Creating a constructor with a string parameter

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

www.perfnurt.se

RE: Creating a constructor with a string parameter

(OP)
Thanks a lot. It works now :D

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

Thanks

Gary

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close