Sign in to follow this  
SpiritX

Help on some little C++ things

Recommended Posts

SpiritX    122
Hi, Im trying to write a new class that has a method that takes string arguements such as bool resume::write(string name, resume add) but it keeps giving me a 16 C:\Dev-Cpp\database.h expected `;' before '(' token 75 C:\Dev-Cpp\database.cpp no `bool resume::write(std::string, resume)' member function declared in class `resume' error whats going on ? Im using <string> and if I do it with an int type parameter it works and my resume class is fine. Also, is there any good programming IRC channels I can get real time help ? Thanks! PS. Im trying to make a window with some text boxes where a user can input data and then save it to a file by pushing a button (kind of like a form for a database). How would I go about doing this? Any tutorials for this?

Share this post


Link to post
Share on other sites
LewsTherin    139
First, it should be "bool resume::write(std::string, std::string resume)" in your .cpp file.

Second, you will need to place "bool write(std::string, std::string)" in your class.

Share this post


Link to post
Share on other sites
SpiritX    122
my bool resume::write(string name, resume add) resume is its own class. The write function is taking a string parameter and a resume parameter.

Share this post


Link to post
Share on other sites
LewsTherin    139
Quote:
Original post by SpiritX
my bool resume::write(string name, resume add) resume is its own class. The write function is taking a string parameter and a resume parameter.


Oh, I see now. Can you post that part of the header file (including a few lines above the function declaration) please?

EDIT: so, lines 14-18 from resume.h

Share this post


Link to post
Share on other sites
SpiritX    122

//resume.h
#ifndef RESUME_H
#define RESUME_H

#include <fstream>
#include <string>


class resume
{
public:
resume();
resume(int nm, int idd);
int getName();
int getID();

bool write(string name);

private:
int name;
int id;
};

#endif





(editted it a bit)

and im trying to do something like
resume a();
a.write("1.txt")


and it gives me an error

16 C:\Dev-Cpp\resume.h expected `;' before '(' token
C:\Dev-Cpp\database.cpp In member function `bool database::addResume()':
74 C:\Dev-Cpp\database.cpp 'class resume' has no member named 'write'


Also.. how do i define the size of an array after I initialized it
such as in java you could do it like :

int a[];
a = new int(4) (or somethign like that)

how do you do this in C++?

Share this post


Link to post
Share on other sites
LewsTherin    139
First, I'll rewrite your code a bit (with comments):


//resume.h
#ifndef RESUME_H
#define RESUME_H

#include <fstream>
#include <string>

// If you don't type this, you must use std::string rather than string
using namespace std;

// Capitalize class names. It helps others (and you) to read your code
class Resume
{
public:
Resume();
Resume(int nm, int idd);
int getName();
int getID();

bool write(string name);

private:
int name;
int id;
};

#endif





One thing I noticed is that you keep changing the defintion for write(). Should it take one argument, or two? In the form above, here is how it would be written in the .cpp:


#include <string>
using namespace std;

...

bool Resume::write(string name){
...




And here is how it would be used:


Resume object;
object.write("Stuff to be written here");




Quote:
Original post by SpiritX
Also.. how do i define the size of an array after I initialized it
such as in java you could do it like :

int a[];
a = new int(4) (or somethign like that)

how do you do this in C++?


You have to use pointers and the "new" operator. For example:



int a*;
a = new int[4];


Share this post


Link to post
Share on other sites
SpiritX    122
Yeah thats how I wrote my resume.h and resume.cpp

Is there something wrong with passing strings as parameters?

And thanks for all the help :D

Share this post


Link to post
Share on other sites
LewsTherin    139
Are you sure that's how you wrote them? It's not matching up the function in the .h with how you're calling it. The way you said it works if you pass an int parameter makes me think you maybe forgot the "using namespace std" part.

Share this post


Link to post
Share on other sites
SpiritX    122
Oh, yeah you were right I had using namespace std for my .cpp but not in my .h haha,

It works now thanks, but now I got another problem. Im using fstream and im trying to make a loop to make a bunch of files and I tried this

string filename;
for(int i = 0 ; i < 5 ; i++)
{
filename = "" + i + ".txt"; < which doesn't work
ofstream outfile(filename);
outfile << 3;
outfile.close();
}

how would I rewrite somethign like this?

Share this post


Link to post
Share on other sites
LewsTherin    139
Quote:
Original post by SpiritX
Im using fstream and im trying to make a loop to make a bunch of files and I tried this

string filename;
for(int i = 0 ; i < 5 ; i++)
{
filename = "" + i + ".txt"; < which doesn't work
ofstream outfile(filename);
outfile << 3;
outfile.close();
}

how would I rewrite somethign like this?


C++ doesn't know how to add integers and strings together. You could try this code:


string filename;
for (int i = 0; i < 5; i++)
{
filename = ".txt";
// Insert 1 copy of a character at position 0 in the string
// The character is represented by an ASCII code. The ASCII code for
// digits 0-9 can be found by the digit + 48.
filename.insert(0, 1, i + 48);

// Do file IO stuff here
}




That will only work if you're going to use 0-9 as values for i. Anything other than that and you will probably need to write (or find) a function that will convert numbers to strings.

Share this post


Link to post
Share on other sites
SpiritX    122
alright thanks that worked but it seems that ifstream infile(filename) doesn't work because ifstream cannot accept a string as a file name?

Share this post


Link to post
Share on other sites
LewsTherin    139
Quote:
Original post by SpiritX
alright thanks that worked but it seems that ifstream infile(filename) doesn't work because ifstream cannot accept a string as a file name?


There are two types of string in C++. The first, oldest, and most annoying is C-style strings. Those are basically just arrays of characters. C++ strings (std::string) is a bolt-on solution to this. If you want to convert from std strings to C strings, the string class has a function to do so - c_str(). So, string.c_str() would return a C-string version of itself.

I just rambled. Maybe I should go to bed...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this