Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 4, 2013, 06:21 AM   #1
eyalt
macrumors newbie
 
Join Date: Jan 2013
question about CPP programing

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 robbieduncan; Jan 4, 2013 at 06:34 AM. Reason: Please use the nifty code tags!
eyalt is offline   0 Reply With Quote
Old Jan 4, 2013, 07:48 AM   #2
ChristianJapan
macrumors Demi-God
 
ChristianJapan's Avatar
 
Join Date: May 2010
Location: 日本
Quote:
Originally Posted by eyalt View Post
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;   // why not double ??
    circle1 *r = nil;    // declare a pointer to a new object
    cout<<"enter numbers";
    cin>>x>>y;
    // r.circle1(x);. // don't call constructor yourself
    r = new circle1(x);  // here you create dynamic an instance and indirect call a constructor

    delete r;    // cleanup 
    r = nil;      // to be safe ;-) 
    return 0;
}
class:
Code:
#ifndef CIRCLE1_H_INCLUDED
#define CIRCLE1_H_INCLUDED

class circle1
{
    private:int r;    // double ??
    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);
     clch(x);
     clcs(x);
    }
}

void circle1::clch(int x)
{
    x=x*2*M_PI;      // wouldn't hurt to declare a local var for the result
    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.
__________________
Member of MacRumors.com Folding@Home Team (#3446) & developer of the F@H Mobile Monitoring app
ChristianJapan is offline   0 Reply With Quote
Old Jan 4, 2013, 08:15 AM   #3
ChristianJapan
macrumors Demi-God
 
ChristianJapan's Avatar
 
Join Date: May 2010
Location: 日本
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 !
__________________
Member of MacRumors.com Folding@Home Team (#3446) & developer of the F@H Mobile Monitoring app

Last edited by ChristianJapan; Jan 4, 2013 at 04:55 PM.
ChristianJapan is offline   0 Reply With Quote
Old Jan 4, 2013, 11:47 AM   #4
danwilliams
macrumors member
 
Join Date: Sep 2008
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";

}
danwilliams is offline   0 Reply With Quote
Old Jan 7, 2013, 03:06 AM   #5
paulCC
macrumors member
 
Join Date: Nov 2012
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

Quote:
Originally Posted by eyalt View Post
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 balamw; Jan 7, 2013 at 10:58 AM. Reason: Please use code tags and trim quotes
paulCC is offline   0 Reply With Quote
Old Jan 7, 2013, 05:37 AM   #6
ChristianJapan
macrumors Demi-God
 
ChristianJapan's Avatar
 
Join Date: May 2010
Location: 日本
Quote:
Originally Posted by paulCC View Post
- 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.
__________________
Member of MacRumors.com Folding@Home Team (#3446) & developer of the F@H Mobile Monitoring app
ChristianJapan is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Color ls terminal output for .h, .c, .cpp files robertneville77 OS X 10.8 Mountain Lion 3 Sep 22, 2013 07:41 PM
Linking to cpp-netlib Wnt2bsleepin Mac Programming 1 Aug 8, 2013 01:51 PM
what is the best Way to start programing? DavidBlack Wasteland 9 May 22, 2013 09:17 PM
Help with xcode (cpp file wont build) Hydrocity Mac Programming 1 Oct 5, 2012 04:37 AM
After System Update last night, the system will not boot with IOPlatformExpert.cpp:15 wonderingwhywhy iMac 1 Jun 12, 2012 10:03 AM

Forum Jump

All times are GMT -5. The time now is 04:02 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC