LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-07-2006, 02:01 PM   #1
websinger
Member
 
Registered: Jul 2003
Location: Virginia
Distribution: LMDE-2 3.11.2amd64
Posts: 35

Rep: Reputation: 15
Indirection in class member declaration


Hi;
In the C++ code fragment below I would like to
declare plotter as being either if type
XPlotter or GIFPlotter depending on the value of "plot"

Code:
...
char *myplotter;
char *my_out;
if(!strcmp(plot,"X")){
   myplotter="XPlotter";
   my_out="cout";
}
else{
   myplotter="GIFPlotter";
   myout="somefile.gif";
}

myplotter plotter(cin,my_out,cerr, params); //<line 68>  declare 'plotter' here
...
...
this is what I get in compiling:
Quote:
g++ fractal.c++ -o fractal
fractal.c++: In function 'int main(int, char**)':
fractal.c++:68: error: expected `;' before 'plotter'
fractal.c++:68: warning: statement has no effect
I assume ( silly me ) that what I want here is some sort of indirection. but either I'm not setting up the pointers right
( not unlikely ) or I've badly misunderstood the syntax here.

Thanks for any assistance

Jeff
 
Old 09-07-2006, 03:39 PM   #2
mjones490
Member
 
Registered: Sep 2005
Distribution: LFS
Posts: 60

Rep: Reputation: 22
What is the function signature of the function plotter? Also, you declare and define char* myplotter. What is the purpose of this variable?
 
Old 09-07-2006, 03:52 PM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by websinger

I assume ( silly me ) that what I want here is some sort of indirection. but either I'm not setting up the pointers right
( not unlikely ) or I've badly misunderstood the syntax here.

Thanks for any assistance

Jeff
You've badly misunderstood the syntax and the difference between runtime strings vs compile time variables names.

When you declare a variable you have to give an actual type. Putting the name of a variable which holds a string won't work because the string isn't there during compile time. If you want to decide the type of plotter at compile time you can probably do so with macros, but that probably isn't what you want.

The proper way of doing this is to define a Plotter class that is a superclass of XPlotter and GIFPlotter and declare plotter as a pointer to type Plotter. A pointer of a superclass may point to an object of any of its subclasses.
 
Old 09-07-2006, 03:54 PM   #4
websinger
Member
 
Registered: Jul 2003
Location: Virginia
Distribution: LMDE-2 3.11.2amd64
Posts: 35

Original Poster
Rep: Reputation: 15
Re: Plotter

Hi
these are from Gnu's plotutils package specifically
the plotter class ( c++ plotting library )
from <plotter.h>
Quote:
/* PLOTTER CTORS (new-style, thread-safe) */
Plotter (FILE *infile, FILE *outfile, FILE *errfile, PlotterParams &params);
Plotter (FILE *outfile, PlotterParams &params);
Plotter (istream& in, ostream& out, ostream& err, PlotterParams &params);
Plotter (ostream& out, PlotterParams &params);
Plotter (PlotterParams &params);
myplotter is a variable which will carry either the values of
"XPlotter" "GIFPlotter"
the end of which is to convey on (my line 68)

Code:
XPlotter plotter(cin,cout,cerr,params);
or
GIFPlotter plotter(cin,somefile.gif,cerr,params);
what I wish of course is to be able to choose whether
to output to the screen or to a file.

Jeff

Last edited by websinger; 09-07-2006 at 04:08 PM.
 
Old 09-07-2006, 04:00 PM   #5
websinger
Member
 
Registered: Jul 2003
Location: Virginia
Distribution: LMDE-2 3.11.2amd64
Posts: 35

Original Poster
Rep: Reputation: 15
re: Plotter class

ntubski:
yes- but it is a plotter class ( see above )

Quote:
The proper way of doing this is to define a Plotter class that is a superclass of XPlotter and GIFPlotter and declare plotter as a pointer to type Plotter. A pointer of a superclass may point to an object of any of its subclasses.
this is what I was attempting however ineffectively
 
Old 09-07-2006, 07:28 PM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by websinger
myplotter is a variable which will carry either the values of
"XPlotter" "GIFPlotter"
the end of which is to convey on (my line 68)
What I was trying to say in my first post is that you can't use a variable like that. The syntax for declaring a variable is
Code:
 <TYPE_NAME> <VAR_NAME>;
neither of these can be substituted with an expression that returns a string. You have to put an actual type name and a variable name. If you could use a variable in this way you would have to write declarations with type names in quotes like this:
Code:
 "GIFPlotter" plotter
But you don't write them like this because type names are not strings.


Quote:
Originally Posted by websinger
Hi
these are from Gnu's plotutils package specifically
the plotter class ( c++ plotting library )
from <plotter.h>

/* PLOTTER CTORS (new-style, thread-safe) */
Plotter (FILE *infile, FILE *outfile, FILE *errfile, PlotterParams &params);
Plotter (FILE *outfile, PlotterParams &params);
Plotter (istream& in, ostream& out, ostream& err, PlotterParams &params);
Plotter (ostream& out, PlotterParams &params);
Plotter (PlotterParams &params);
Quote:
Originally Posted by websinger
ntubski:
yes- but it is a plotter class ( see above )

Quote:
The proper way of doing this is to define a Plotter class that is a superclass of XPlotter and GIFPlotter and declare plotter as a pointer to type Plotter. A pointer of a superclass may point to an object of any of its subclasses.
this is what I was attempting however ineffectively
What I didn't realize until now, is that XPlotter and GIFPlotter are also plotutils classes. I thought they were your own which is why I was telling you that you should subclass Plotter.

All you have to do is declare plotter as a Pointer to Plotter:
Code:
...
Plotter *plotter; //declare 'plotter' here

if(!strcmp(plot,"X")){
   //Instances of the XPlotter and XDrawablePlotter classes always ignore the output stream       
   //argument, since they write graphics to an X Display rather than to a stream.
   plotter = new XPlotter(cin,NULL,cerr,params);
}
else{
   FILE *gif_file;
   gif_file = fopen("somefile.gif", "w");

   //add some error checking here...

   plotter = new GIFPlotter(cin,gif_file,cerr,params);
}
...
Note: I left params in both the constructors although according to the plotutils manual there is no such parameter.
 
Old 09-07-2006, 07:52 PM   #7
websinger
Member
 
Registered: Jul 2003
Location: Virginia
Distribution: LMDE-2 3.11.2amd64
Posts: 35

Original Poster
Rep: Reputation: 15
Re: *plotter

Ntubski - Thanks I'll try that, this is actually closer to my
initial efforts, but I kept loosing scope from my function being declared inside the conditional. The pointer should address that.

I think parts of the Plotutils doc are somewhat out of date
the listing of the constructors in my reply to mark jones
is copied right out of the header file.

I've been using the plotutils code for XPlotter for a couple of
years. but have had to write separate programs to view or save to file . This iteration I'm trying to consolidate the code, and hopefully increase my skills on o-o syntax. Thanks for the assist.

Jeff
 
Old 09-08-2006, 12:10 AM   #8
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
You want to first declare plotter as a pointer to the base class
Code:
Plotter *plotter;
Then within your if instantiate the object that you want
Code:
if(!strcmp(plot,"X"))
{
   plotter = new XPlotter();
   my_out="cout";
}
else
{
   plotter = new GIFPlotter();
   myout="somefile.gif";
}
Well that's the theory, I'll leave you the correct any mistakes I made!
 
Old 09-08-2006, 10:24 AM   #9
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by websinger
Ntubski - Thanks I'll try that, this is actually closer to my
initial efforts, but I kept loosing scope from my function being declared inside the conditional. The pointer should address that.
Just make sure you delete the object after you finish otherwise you get a memory leak

Quote:
I think parts of the Plotutils doc are somewhat out of date
the listing of the constructors in my reply to mark jones
is copied right out of the header file.
Yeah, only the old manual was online, and I didn't feel like downloading the whole package...

Quote:
I've been using the plotutils code for XPlotter for a couple of
years. but have had to write separate programs to view or save to file . This iteration I'm trying to consolidate the code, and hopefully increase my skills on o-o syntax. Thanks for the assist.

Jeff
Glad I could help
 
  


Reply

Tags
pointers



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
passing a class member function to pthread_create. dmail Programming 1 07-29-2006 11:15 AM
C++ - problem with pointers to class member functions Nylex Programming 3 01-15-2006 10:14 AM
Array declaration in class or main function??? redhatrosh Programming 4 03-15-2005 02:13 PM
Difference between including a header file and 'class' declaration in C++ scoTtkIm Programming 1 09-09-2004 04:23 AM
a class with a ifstream member Hano Programming 2 04-24-2002 11:01 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration