Hello,

I am trying to get the return value from the next_number function up to the makefile function. I am getting an error that says next_number does not have a class type.

EDIT: the error is "return statement with a value, in function returning 'void'"

I don't understand why it says the function is void?

next_number is a private function. Do I need to do something different since it's private?

#include "utility.h"
#include "filer.h"

filer::filer()
{
    //ctor
}

filer::~filer()
{
    //dtor
}

//precondition: n, range, truly_random, and file_name have
//been set; truly_random is type bool, and determines whether
//the numbers are truly random, or pseudorandom
//postcondition: n random integers in the range 0-range,
//inclusive, have been written to a file named file_name,
//one per line;
void filer::makefile(int n, int range, bool truly_random, string file_name)
{
    ofstream outstream;
    outstream.open(file_name.c_str());

        if (!truly_random){
            for(int i = 0; i < n; i++){
                outstream << next_number() << endl;
            }
        }
        else if (truly_random){
            srand(int(time(NULL)));

            for(int i = 0; i < n; i++){
                outstream << next_number() << endl;
            }
        }
        else {
            cout << "Improper choice." << endl;
        }
    outstream.close();   //Close the file

//precondition: range has been set to a nonnegative value
//postcondition: returns a random integer in the range
//0-range, inclusive
int next_number(int range);
    int value = rand() % range + 1;[LIST=1]
[/LIST]
    return value;

}

Recommended Answers

All 15 Replies

write a function that will solve the series: x+(x^2/2)+(x^3/3)+(x^4/5)+(x^5/7)+......(x^n/k) .where k are all prime numbers

write a function that will solve the series: x+(x^2/2)+(x^3/3)+(x^4/5)+(x^5/7)+......(x^n/k) .where k are all prime numbers

How will that help?

Look closer at line 45

I don't think there's any need to think about prime numbers.

caut_baia,

Thank you for the tip. I have changed line 45 to:

int filer::next_number(int range);

I now get the error "invalid use of qualified-name 'filer::next_number'

I think I need to use "this", but I am having trouble remembering how that works.

You need to look at the syntax of a function definition first.This is something you should've known before beginning the study of classes.I just pasted the first google match but it should do http://msdn.microsoft.com/en-us/library/c4d5ssht.aspx

I see another dumb mistake I made (ending line 45 with ";")

int next_number(int range)
{
    int value = rand() % range + 1;    return value;
 
}

Is that what you were referring to with the google search?

I also have a filer.h file where I have declared the next_number function:

#ifndef FILER_H
#define FILER_H
//header file FILER.H for class filer
//a class object can create a file of a specified
//number of random integers in a specified range

class filer
{
public:
filer(); //NOT ORIGINAL
void makefile(int n, int range, bool truly_random, string file_name);
//precondition: n, range, truly_random, and file_name have
//been set; truly_random is type bool, and determines whether
//the numbers are truly random, or pseudorandom
//postcondition: n random integers in the range 0-range,
//inclusive, have been written to a file named file_name,
//one per line;

virtual ~filer(); //NOT ORIGINAL
private:
    int next_number(int range);
    //precondition: range has been set to a nonnegative value
    //postcondition: returns a random integer in the range
    //0-range, inclusive
};
#endif //FILER_H

Did I make an error in the filer.h by putting the destructor on line 19 instead of the end of the file?

The program does not run when I put the destructor after the private function.

Destructors should be public otherwise they cannot be called.

I'm going to have to call Unsolved Mysteries on this one

What's so mysterious ?

I found my other dumb mistake. Missed a } at the end of the makefile function.

I've been trying to get the random number generator to include 0. Is this possible?

There's another problem I have not been able to figure out. I am supposed to use this filer.h file with no changes. To get the program to work, I added lines 10 and 19. I am not supposed to add these lines, but without them I get the error on line 4:

"definition of implicitly-declared 'filer::~filer()'"

#ifndef FILER_H
#define FILER_H
//header file FILER.H for class filer
//a class object can create a file of a specified
//number of random integers in a specified range

class filer
{
public:
filer(); //NOT ORIGINAL
void makefile(int n, int range, bool truly_random, string file_name);
//precondition: n, range, truly_random, and file_name have
//been set; truly_random is type bool, and determines whether
//the numbers are truly random, or pseudorandom
//postcondition: n random integers in the range 0-range,
//inclusive, have been written to a file named file_name,
//one per line;

virtual ~filer(); //NOT ORIGINAL
private:
    int next_number(int range);
    //precondition: range has been set to a nonnegative value
    //postcondition: returns a random integer in the range
    //0-range, inclusive
};
#endif //FILER_H
#include "utility.h"
#include "filer.h"

filer::filer()
{
    //ctor
}

filer::~filer()
{
    //dtor
}

//precondition: n, range, truly_random, and file_name have
//been set; truly_random is type bool, and determines whether
//the numbers are truly random, or pseudorandom
//postcondition: n random integers in the range 0-range,
//inclusive, have been written to a file named file_name,
//one per line;
void filer::makefile(int n, int range, bool truly_random, string file_name)
{
    ofstream outstream;
    outstream.open(file_name.c_str());

        if (!truly_random)
        {
            for(int i = 0; i < n; i++)
            {
                outstream << next_number(range) << endl;
            }
        }
        else if (truly_random)
        {
            srand(int(time(NULL)));

            for(int i = 0; i < n; i++)
            {
                outstream << next_number(range) << endl;
            }
        }
        else
        {
            cout << "Improper choice." << endl;
        }

    outstream.close();   //Close the file
}
    //cout << next_number.this->value;

//precondition: range has been set to a nonnegative value
//postcondition: returns a random integer in the range
//0-range, inclusive
int filer::next_number(int range)
{
    int value = rand() % range + 1;
    return value;
}

The program does not run when I put the destructor after the private function.

Destructor must be in the public section to enable it to destruct the object after its job.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.