0

I am very new to C++, coming form Java and C. My book does not mention private functions and Google searches don't turn up much. This should be trivial for me, but I can't get it to work.

I have this code:

#ifndef RUNDATABASE_H
#define RUNDATABASE_H
#include <iostream>
#include <string>

class RunDatabase
{
    public:
        int main();
    protected:
    private:
        bool checkIfProperID(std::string);
};

#endif // RUNDATABASE_H

And in another file:

#include "RunDatabase.h"

int main()
{

    std::string id; // after this, I initialize id

    if(!checkIfProperID(id))
    {
        std::cout << "Improperly formatted student ID, must be numeric" << std::endl;
        break;
    }

}

bool RunDatabase::checkIfProperID(std::string id)
{
    return true;
}

I get this error: error: 'checkIfProperID' was not declared in this scope

Using MinGW g++ 4.4.1 on Windows 7 64 bit.

Thanks for any help.

jn1kk
  • 5,012
  • 2
  • 45
  • 72
  • 2
    Shouldn't you know this if you came from Java? Sorry. Anyways, the answer will be pretty clear. – Marlon Mar 07 '12 at 02:08
  • @Marlon Since the main is in the same class as checkIfProperId, I don't see why the other function is not in the scope. You can do the same thing in Java from main, but the method must obviously be static or you need do instantiate a new object of the same class. – jn1kk Mar 07 '12 at 02:10
  • If your book doesn't mention private functions you should *really really* drop it and get [a book worthy of that name](http://stackoverflow.com/q/388242/46642). – R. Martinho Fernandes Mar 07 '12 at 02:18
  • @R.MartinhoFernandes "C++ Programming With Design Patterns Revealed" by Tomasz Muldner, only mentions private fields. But this error stemmed from the fact that I did now know the main function was not part of the class. – jn1kk Mar 07 '12 at 02:25

3 Answers3

1

checkIfProperID is a method of RunDatabase. This means you need to have a RunDatabase object in order to call checkIfProperID.

RunDatabase rd;
rd.checkIfProperID(id);

I don't see why the other function is not in the scope.

The "scope" here is the class.

RunDatabase::checkIfProperID

Notice the scope-resolution operator ::. This means that the method belongs to the class, not global scope.

Marlon
  • 19,924
  • 12
  • 70
  • 101
  • So the main function is not part of the class? – jn1kk Mar 07 '12 at 02:13
  • 1
    @skynorth No. The entry point `main` in C and C++ are declared in the global scope. You have a misconception that `RunDatabase::main` is the entry point. `RunDatabase::main` is not the same as `main`. – Marlon Mar 07 '12 at 02:14
  • I am learning C++ from the book "C++ Programming With Design Patterns Revealed" by Tomasz Muldner, and nowhere is this mentioned. Thanks! You can see how I was confused since main is part of the class in Java. Accept answer in a sec. – jn1kk Mar 07 '12 at 02:15
1

Unlike Java, C++ allows free-standing functions. The main function that is called when you run your program is free-standing main, not a member main. If you modify your cpp file as follows, things should compile:

int main() {
    RunDatabase rdb;
    rdb.main();
}

RunDatabase::main() {
    // the code of the main function from your post
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Sorry, can't accept your answer since someone already gave me an answer. But this is something I will use. +1 – jn1kk Mar 07 '12 at 02:17
1

The problem is that main is not implemented as a member of RunDatabase.

int main()
{

should be

int RunDatabase::main()
{

You will then need a main() function, which your program will start execution in.

Also consider not naming your class member functions after the main function which starts execution, to avoid confusion. Example:

class RunDatabase
{
public:
    int execute();
protected:
private:
    bool checkIfProperID(std::string); 
};

int RunDatabase::execute()
{

    std::string id; // after this, I initialize id

    if(!checkIfProperID(id))
    { 
        std::cout << "Improperly formatted student ID, must be numeric" << std::endl;
        break;
    }

}

/// runs when the program starts
int main()
{
    RunDatabase runDatabase;
    runDatabase.execute();
}
rob05c
  • 1,223
  • 1
  • 9
  • 18