0
votes

//My Header File

//Header File
#include<iostream>
using namespace std;

MyFloat rotate_right(MyFloat obj)
{

    /*All that this function is supposed to do, is to return a new object, that object will
    be having array elements in the order like     2.2   3.2   4.2   5.2  6.2   where the 
    original(object that is passed in parameter) object must be having array elements 
    in order like  3.2   4.2   5.2   6.2   2.2
    */
    MyFloat c;
    c.size = obj.size;
    c.arr = nullptr;
    c.arr = new float [obj.size];
    c.arr[0] = obj.arr[obj.size - 1];
    for(int i = 1,j=0; i < (obj.size - 1); i++,j++)
    {
        c.arr[j] = obj.arr[i];
    }
    return c;
}


class MyFloat
{
    public:
    MyFloat();//Default Constructor
    MyFloat(int data);//Parameterized Constructor
    MyFloat(MyFloat & const obj);//Copy Constructor
    void input();//Filling Array
    void PrintFloat();//For Displaying Array
    MyFloat& operator=(MyFloat & const obj);//Assignment Operator Overloading
    friend MyFloat rotate_right(MyFloat obj);/////////////This is the Function which is causing Problem/////////////
    ~MyFloat();//Destructor

    private:
    int size;//size of array
    float *arr;

};

////////////This is the Cpp FILE Having Definition of All Functions

#include"My_Floats_Header.h"



MyFloat::MyFloat()
{
    cout << "Default Constructor Called!" << endl;
    size = 0;
    arr = nullptr;
}

MyFloat::MyFloat(int _size)
{
    cout << "Parametrized Constructor Called!" << endl;
    size = _size;
    arr = nullptr;
    arr = new float [_size];
}

void MyFloat :: input()
{
    for(int i = 0 ; i < size; i++)
    {
        cout << "Enter an Element = ";
        cin >> arr[i];
    }
}

MyFloat :: MyFloat(MyFloat & const obj)
{
    size = obj.size;
    arr = nullptr;
    arr = new float [size];
    for(int i = 0 ; i < obj.size; i++)
    {
        arr[i] = obj.arr[i];
    }
}

MyFloat &  MyFloat :: operator=(MyFloat & const obj)
{
    size = obj.size;
    arr = nullptr;
    arr = new float [size];
    for(int i = 0 ; i < obj.size; i++)
    {
        arr[i] = obj.arr[i];
    }
    return *this;
}

void MyFloat :: PrintFloat()
{
    for(int i = 0 ; i < size; i++)
    {
        cout << arr[i];
    }
    cout << endl;
}

MyFloat::~MyFloat()
{
    if(arr != nullptr)
    {
        delete [] arr;
    }
    size = 0;
}

////Cpp file having mian()

#include"My_Floats_Header.h"


int main()
{
    MyFloat *floatNumber = new MyFloat(5);//declaration
    floatNumber -> input();//taking input
    MyFloat newfloatNumber = *floatNumber;
    floatNumber = rotate_right(floatNumber);

    cout << "My Float without Rotation: ";
    newfloatNumber.PrintFloat();

    cout << "My Float after Rotation: ";
    floatNumber -> PrintFloat();
    system ("pause");
    return 0;
}

ERRORS THAT I'm GETTING:

Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int (in line number 4)

Error C2146 syntax error: missing ';' before identifier 'rotate_right' (in line number 4)

Error C2143 syntax error: missing ';' before '{' (in line number 5)

Error C2447 '{': missing function header (old-style formal list?) (in line number 5)

Error C2143 syntax error: missing ';' before '&' (in line number 40)

Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int (in line number 40)

Error C2086 'int MyFloat': redefinition (in line number 40)

Error C2761 'MyFloat &MyFloat::operator =(MyFloat &)': member function redeclaration not allowed (in line number 41)

Error C2059 syntax error: '{' (in line number 41)

Error C2143 syntax error: missing ';' before '{' (in line number 41)

Error C2447 '{': missing function header (old-style formal list?) (in line number 41)

Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int (in line number 4)

Error C2146 syntax error: missing ';' before identifier 'rotate_right' (in line number 4)

Error C2143 syntax error: missing ';' before '{' (in line number 5)

Error C2447 '{': missing function header (old-style formal list?) (in line number 5)

Error C2065 'floatNumber': undeclared identifier (in line number 6)

Error C2061 syntax error: identifier 'MyFloat' (in line number 6)

Error C2065 'floatNumber': undeclared identifier (in line number 7)

Error C2227 left of '->input' must point to class/struct/union/generic type (in line number 7)

Error C2146 syntax error: missing ';' before identifier 'newfloatNumber' (in line number 8)

Error C2065 'newfloatNumber': undeclared identifier (in line number 8)

Error C2065 'floatNumber': undeclared identifier (in line number 8 )

Error C2065 'floatNumber': undeclared identifier (in line number 9)

Error C2065 'newfloatNumber': undeclared identifier (in line number 12)

Error C2228 left of '.PrintFloat' must have class/struct/union (in line number 12)

Error C2065 'floatNumber': undeclared identifier (line number 15)

Error C2227 left of '->PrintFloat' must point to class/struct/union/generic type (line number 15)

2
Dude - will you at least add a guard to your header files? Linking h files with ifdef header guards. I'd also urge you to consider using forward declarations as a more robust solution to "chicken/egg" problems like you encountered here.paulsm4

2 Answers

-1
votes

In the header file, move the class definition above the definition of rotate_right(). This will get you closer to working code. Order matters.

1
votes

MyFloat rotate_right() probably shouldn't be in a header file. If you take it out, you should resolve (most? all?) of your errors.

My recommendation would be to make it a static method of MyFloat.

As an alternative, you could keep rotate_right() as a standalone function, and simply #include "MyFloat.h at the top of your .cpp file.