简体   繁体   中英

Class object is 'undefined' in int main()

I have two .cpp files "rational.cpp" and "main.cpp" and one "rational.h" file. In "rational.cpp" I have a declared a Rational class, and included "rational.h" at the top of both files; however when I try to create the object Rational rat; . I get the error 'identifier "Rational" is undefined'. I am very confused.

Here is "main.cpp"

#include "rational.h"


int main() {
    int select = 1, n, d;

    while (select != 0) {
        cout << "Enter numerator";
        cin >> n;
        cout << "\nEnter denominator";
        cin >> d;
        try {
            if (d == 0) { 
                throw 0;
            }
        }
        catch (int e) {
            cout << "/nerror: cannot divide by 0" << endl;
            cout << "enter a new denominator: ";
            cin >> d;
        }

        Rational rat;

        cout << "1. Add a rational\n2. Subtract a rational\n3. Multiply by a rational\n4. Divide by a rational\n0. Exit";
        cout << "enter selector" << endl;
        cin >> select;
        switch (select) {
        case 1: rat.add(n, d); break;
        case 2: rat.sub(n, d); break;
        case 3: rat.mul(n, d); break;
        case 4: rat.div(n, d); break;
        case 0: break;
        default:
            try {
                throw select;
            }
            catch (int e) {
                cout << e << " is not a valid option" << endl;
            }
            catch (...) {
                cout << "error: invalid option" << endl;
            }
            break;
        }

    }
    return 0;

}

"rational.cpp"

#include "rational.h"


class Rational{
public:

    void reduce(int n, int d);
    //define math functions
    void add(int n, int d);
    void sub(int n, int d);
    void div(int n, int d);
    void mul(int n, int d);
};

void Rational::reduce(int n, int d) {


}

void Rational::add(int n, int d) {


}

void Rational::sub(int n, int d) {


}

void Rational::div(int n, int d) {


}

void Rational::mul(int n, int d) {


}

"rational.h"

#pragma once
#ifndef RATIONAL
#define RATIONAL

#include <iostream>
#include <math.h>
#include <exception>

using namespace std;

#endif

and my Makefile

rational.exe:   main.o  rational.o  
    g++ -o  rational.exe    main.o  rational.o

main.o: main.cpp    rational.h
    g++ -c  main.cpp 

rational.o: rational.cpp    rational.h
    g++ -c  rational.cpp

I am using cygwin to compile.

The declaration of rational has to be inside the header, or else the compiler won't read it (for lack of a better word) in main.cpp.

You need to move the declaration (ie the following portion) to rational.h:

class Rational{
public:

    void reduce(int n, int d);
    //define math functions
    void add(int n, int d);
    void sub(int n, int d);
    void div(int n, int d);
    void mul(int n, int d);
};

For more information, see this question .

Although you included Rational.h in you main.cpp file, Rational.h doesn't contain any definition/declaration of the Rational class. declare / define the Rational class in the Rational.h file and keep all the functions implementation in you cpp file. Add

class Rational;

to you header file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM