Jump to content
  • Advertisement
Sign in to follow this  
SpiritX

Help on some little C++ things

This topic is 4881 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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
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
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

//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
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
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
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
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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!