Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

eyalt

macrumors newbie
Original poster
Jan 4, 2013
1
0
hi, i tried to compile this program and the compiler gave me this massage: "invalid use of 'class circle1'.
im quiet new at progrming and i have no idea what i did wrong.
any help will be appricieted...

this is my code:
main:
Code:
#include <iostream>
#include <fstream>
#include "circle1.h"
#include "tri.h"
using namespace std;

int main()
{
    int x,y;
    circle1 r;
    cout<<"enter numbers";
    cin>>x>>y;
    r.circle1(x);
    return 0;
}

class:
Code:
#ifndef CIRCLE1_H_INCLUDED
#define CIRCLE1_H_INCLUDED

class circle1
{
    private:int r;
    public:
    void clch(int);
    void clcs(int);
    circle1(int);
    circle1();
};



#endif // CIRCLE1_H_INCLUDED

header:
Code:
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include "circle1"
using namespace std;

circle1::circle1(int x)
{

    if(x<0)
    cout<<"the number you enterd is negative!";
    else
    {
    r=x;
    void clch(int x);
    void clcs(int x);
    }
}

void circle1::clch(int x)
{
    x=x*2*M_PI;
    cout<<"this is the hikef of the first circle: "<<x<<"\n";
}

void circle1::clcs(int x)
{
    x=2*M_PI*(pow(x,2));
    cout<<"this is the sheth of the first circle: "<<x<<"\n";

}
 
Last edited by a moderator:

ChristianVirtual

macrumors 601
May 10, 2010
4,122
282
日本
hi, i tried to compile this program and the compiler gave me this massage: "invalid use of 'class circle1'.
im quiet new at progrming and i have no idea what i did wrong.
any help will be appricieted...

this is my code:
main:
Code:
#include <iostream>
#include <fstream>
#include "circle1.h"
#include "tri.h"
using namespace std;

int main()
{
    [B]int x,y;   // why not double ??[/B]
    [B]circle1 *r = nil;    // declare a pointer to a new object[/B]
    cout<<"enter numbers";
    cin>>x>>y;
    [B]// r.circle1(x);[/B]. // don't call constructor yourself
    [B]r = new circle1(x);  // here you create dynamic an instance and indirect call a constructor

    delete r;    // cleanup 
    r = nil;      // to be safe ;-) [/B]
    return 0;
}

class:
Code:
#ifndef CIRCLE1_H_INCLUDED
#define CIRCLE1_H_INCLUDED

class circle1
{
    [B]private:int r;    // double ??[/B]
    public:
    void clch(int);
    void clcs(int);
    circle1(int);
    circle1();
};



#endif // CIRCLE1_H_INCLUDED

header:
Code:
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include "circle1"
using namespace std;

circle1::circle1(int x)
{

    if(x<0)
    cout<<"the number you enterd is negative!";
    else
    {
    r=x; 
    [B]// void clch(int x);
    // void clcs(int x);
     clch(x);
     clcs(x);[/B]
    }
}

void circle1::clch(int x)
{
    x=x*2*M_PI;      [B]// wouldn't hurt to declare a local var for the result[/B]
    cout<<"this is the hikef of the first circle: "<<x<<"\n";
}

void circle1::clcs(int x)
{
    x=2*M_PI*(pow(x,2));
    cout<<"this is the sheth of the first circle: "<<x<<"\n";

}

I got a bit rusty in C++ but I would not declare an instance the way you wrote. Better to use new-method. See the bold remarks in your code; also why you choose Integer for X and r. Double would be better as you will calculate with decimals.
 

ChristianVirtual

macrumors 601
May 10, 2010
4,122
282
日本
ok; little update; fresh from Xcode

main.cpp
Code:
#include <iostream>
#include <fstream>
#include "circle1.h"
//#include "tri.h"

using namespace std;

int main(int argc, const char * argv[])
{

   double x, y;
   
   circle1 *r = NULL;    // declare a pointer to a new object
   
   cout<<"enter numbers";
   cin>>x>>y;
   
   r = new circle1(x);  // here you create dynamic an instance and indirect call you desired constructor
   
   delete r;      // cleanup
   r = NULL;      // to be safe ;-) 

   return 0;
}

circle1.h
Code:
#ifndef __cppmr__circle1__
#define __cppmr__circle1__

#include <iostream>

class circle1
{
   private:
      double r;
   
   public:
      void clch(double);
      void clcs(double);
   
      circle1(double);
      circle1();
};

#endif /* defined(__cppmr__circle1__) */

circle1.cpp
Code:
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include "circle1.h"
using namespace std;

circle1::circle1(double x)
{
   
   if (x < 0)
   {
      cout<<"the number you enterd is negative!";
   }
   else
   {
      r = x;

      clch(x);
      clcs(x);
   }
}

void circle1::clch(double x)
{
   double h = x * 2 * M_PI;      // wouldn't hurt to declare a local var for the result
   cout << "this is the hikef of the first circle: " << h << "\n";
}

void circle1::clcs(double x)
{
   double s = 2*M_PI*(pow(x,2));
   
   cout << "this is the sheth of the first circle: " << s << "\n";
   
}


result in console
Code:
enter numbers2
3
this is the hikef of the first circle: 12.5664
this is the sheth of the first circle: 25.1327

And ... Welcome to MR !
 
Last edited:

danwilliams

macrumors member
Sep 15, 2008
46
0
Code:
#include <iostream>
#include <fstream>
#include "circle1.h"
#include "tri.h"
using namespace std;

int main()
{
    int x,y;
    circle1 r; // default constructor called - do you want to call this constructor and not the other one?
    cout<<"enter numbers";
    cin>>x>>y;
    r.circle1(x); // ERROR - calling the constructor again!
    return 0;
}

I believe this is the first of many errors. See my comments in the code.

The other is the following in the file, "circle1.cpp":
Code:
#include "circle1" // ERROR - you need the extension... "circle1.h"

And this:
Code:
void circle1::clcs(int x)
{
    x=2*M_PI*(pow(x,2)); // ERROR - the function, pow(), takes a double and an int as arguments
    cout<<"this is the sheth of the first circle: "<<x<<"\n";

}
 

paulCC

macrumors regular
Nov 2, 2012
100
59
It's been few years since I have coded in C++, but I think I see the trouble....

You cannot invoke a constructor as a member on an existing instance. So this line:
Code:
    r.circle1(x);

will fail compiling.

Here is the body of the main function, following your style:
Code:
int main()
{
    int x,y;

    cout<<"enter numbers";
    cin>>x>>y;

    // make an instance of circle1 to test it
    circle1 r(x);

    return 0;
}
There are few things to comment on:
- instantiate an object only in the part of the code where you need it. You do not have to instantiate the circle1 before you obtain the input value - you can instantiate it afterwards, using the provided constructor.

- use initialiser list in the constructor. That will make the constructor look like this:
Code:
circle1::circle1(int x)
  :
  r ( x )  // this sets up the r member
{

    if(x<0)
    cout<<"the number you enterd is negative!";
    else
    {
 
    void clch(int x);
    void clcs(int x);
    }
}
The initialiser list should be the way you initialise all your members. It is the ONLY way to initialise const members and other class members.

- do not use the approach with dynamically allocated memory advised in an above post. It is not warranted here. Dynamic memory allocation is needed if you intend to create an object in an function, and have it live outside of the function. This is not the case here.

- The member r of class circle1 should probably be of a floating point type, not integer.

- why do the member functions clch() and clcs() have an input parameter ? It seems they should work on the member r. If not, then why would you want the r member in first place ?

PaulCC

hi, i tried to compile this program and the compiler gave me this massage: "invalid use of 'class circle1'.
im quiet new at progrming and i have no idea what i did wrong.
any help will be appricieted...
 
Last edited by a moderator:

ChristianVirtual

macrumors 601
May 10, 2010
4,122
282
日本
- do not use the approach with dynamically allocated memory advised in an above post. It is not warranted here. Dynamic memory allocation is needed if you intend to create an object in an function, and have it live outside of the function. This is not the case here.

Fair enough; a bit an overkill for this case here. But it also good to learn early the magic of dynamic allocation; and all the headache it might/will cause.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.