简体   繁体   English

类对象在int main()中为'undefined'

[英]Class object is 'undefined' in int main()

I have two .cpp files "rational.cpp" and "main.cpp" and one "rational.h" file. 我有两个.cpp文件“ rational.cpp”和“ main.cpp”,以及一个“ rational.h”文件。 In "rational.cpp" I have a declared a Rational class, and included "rational.h" at the top of both files; 在“ rational.cpp”中,我声明了一个Rational类,并且在两个文件的顶部都包含了“ rational.h”。 however when I try to create the object Rational rat; 但是,当我尝试创建对象Rational rat; . I get the error 'identifier "Rational" is undefined'. 我收到错误“标识符“ Rational”未定义”。 I am very confused. 我很困扰。

Here is "main.cpp" 这是“ 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" “ 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" “ rational.h”

#pragma once
#ifndef RATIONAL
#define RATIONAL

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

using namespace std;

#endif

and my Makefile 和我的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. 我正在使用cygwin进行编译。

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. rational的声明必须在标头中,否则编译器将不会在main.cpp中读取它(因为缺少更好的词)。

You need to move the declaration (ie the following portion) to rational.h: 您需要将声明(即以下部分)移至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. 尽管您在main.cpp文件中包括了Rational.h,但是Rational.h不包含Rational类的任何定义/声明。 declare / define the Rational class in the Rational.h file and keep all the functions implementation in you cpp file. 在Rational.h文件中声明/定义Rational类,并将所有函数实现保留在cpp文件中。 Add

class Rational;

to you header file 给你头文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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