Andrew
Andrew

Reputation: 43

C++ Undeclared Identifier on Object creation

So I am new to c++, coming from C#. This is giving me several errors when compiling, which all seem to relate to this object declaration. Anyone able to show me the right way to do this?

I get an undeclared identifier where i declare tri(sideLength).

I have used this as a reference for object declaration, but it doesn't seem to be helping me.

Thanks.

#include <iostream>    // Provides cout
#include <iomanip>     // Provides setw function for setting output width
#include <cstdlib>     // Provides EXIT_SUCCESS
#include <cassert>     // Provides assert function
#include <stdexcept>
#include <math.h> 

using namespace std;   // Allows all standard library items to be used

void setup_cout_fractions(int fraction_digits)
// Precondition: fraction_digits is not negative.
// Postcondition: All double or float numbers printed to cout will now be
// rounded to the specified digits on the right of the decimal.
{
    assert(fraction_digits > 0);
    cout.precision(fraction_digits);
    cout.setf(ios::fixed, ios::floatfield);
    if (fraction_digits == 0)
        cout.unsetf(ios::showpoint);
    else
        cout.setf(ios::showpoint);
}

int main()
{
    const int MAX_SIDE_LENGTH = 6;
    const int INITIAL_LENGTH = 1;
    const int DIGITS = 4;
    const int ARRAY_SIZE = 6;

    // Set up the output for fractions and print the table headings.
    setup_cout_fractions(DIGITS);

    // Each iteration of the loop prints one line of the table.
    for (int sideLength = 0; sideLength < MAX_SIDE_LENGTH; sideLength += 1)
    {
        EquilateralTriangle tri(sideLength);
        //Square sq(sideLength);
        //Pentagon_Reg pent(sideLength);
        //Hexagon_Reg hex(sideLength);
        //Heptagon_Reg hept(sideLength);
        //Octagon_Reg octa(sideLength);

        cout << "Type: " << tri.Name() << "has area: " << tri.Area() << " with SideLength = " << sideLength;
    }

    return EXIT_SUCCESS;
}

//Template

class GeometricFigure
{
public:
    GeometricFigure() { }
    double SideLength;
    virtual double Area() { return 0; };
    virtual char* Name() { return ""; };
};

class EquilateralTriangle : public GeometricFigure {
public:
    EquilateralTriangle(double sideLength)
    {
        SideLength = sideLength;
    }
    char* Name() { return "Equilateral Triangle"; }
    double Area() { return (sqrt(3) / 2 * pow(SideLength, 2)); }
};

Upvotes: 0

Views: 336

Answers (2)

Prabindh
Prabindh

Reputation: 3504

You would need to "declare" or tell the compiler, where to look for the EquilateralTriangle and GeometricFigure, "before" you use it first. you might want to take a look at the similar discussion at - C# declarations vs definitions

Upvotes: 0

In C++, the compiler reads your code from top-to-bottom, once. This is a holdover from when early C compilers only had a few kilobytes of memory to work with - C was designed so that a compiler would only need to look at a little bit of the code at a time.

Because of this, things must have been declared or defined as necessary, before you try to use them.

Move both classes somewhere before main. GeometricFigure must be before EquilateralTriangle, and EquilateralTriangle must be before main.

Upvotes: 2

Related Questions