* Pointers to classes
@ 2004-06-20 4:46 Eric
2004-06-20 5:06 ` John T. Williams
0 siblings, 1 reply; 6+ messages in thread
From: Eric @ 2004-06-20 4:46 UTC (permalink / raw)
To: linux-c-programming
Hello
First I would like to say im fairly new to C++ and keep in mind you will
probably see many glaring mistakes in reading my code, please be kind. Hey,
thats how we learn :)
My problem is with pointers to classes. I am writing a VPN management program
and have a class Connection, which handles a connection ppp0, ppp1, etc, and
is closely tied with a class ConfigFile, which handles all the variables for
that connection obviously read from a file.
I need to have a pointer to an object ConfigFile inside the Connection class
so I can access variables and such associated with that connection that were
read from a file. However, I am getting very strange compile errors when
trying to define the pointer to a class. These are the errors. Below are the
relevant files and how they are included.
*/home/bot403/programming/projects/vpn_client/src/connection.h:28: error:
syntax error before `*' token
*/home/bot403/programming/projects/vpn_client/src/connection.h:31: error:
parse error before `*' token
*/home/bot403/programming/projects/vpn_client/src/connection.h:36: error:
syntax error before `*' token
Here are the relevant files. Please let me know if you need additional
information.
BTW, I don't consider making Connection a sublass of configfile a solution
becuase I am already teaching myself alot with this project and am happy with
simpleton solutions. Subclasses/Inheritance would require that I teach
myself way too much to accomplish way too little.
---------------------start connection.h
class Connection
{
private:
char ifName[ 5 ];
char pppdopts[256];
char sshopts[256];
char pidfile[256];
ConfigFile * config;
pid_t pid;
public:
Connection(ConfigFile * cFile );
void printName( void );
int start();
int stop();
~Connection();
ConfigFile * getConfig();
};
----------------------- end file
-----------------------start configfile.h
int readElement( char buffer[], char option[], char argument[] );
using namespace std;
class ConfigFile {
private:
ifstream configFile;
char configFileName[ 256 ];
//File variables
char pppdevice[ 8 ];
char vpnhost[ 256 ];
char logfile[ 256 ];
char *pppdopts;
//SSH Specific
char sshcipher[ 15 ];
char sshlogin[ 256 ];
char sshidentity[ 256 ];
char *sshopts;
char sshport[ 10 ];
//Misc
char delay[ 10 ];
char vpnpidfile[80];
//Connection Details
char *networks[5];
char *nameservers[2];
//End File Variables
public:
ConfigFile( char *Name );
~ConfigFile();
int readConfig();
void printConfig();
int writeConfig(char *Filename);
char *getsshopts(char buffer[]);
char *getpppdopts(char buffer[]);
char *getlogfile(char buffer[]);
char *getpidfile(char buffer[]);
//FIXME
//char *getnetworks();
//char *getnameservers();
};
-----------end of file
-----------top of connection.cpp
#include <iostream>
#include <fstream>
#include <unistd.h>
#include "configfile.h"
#include "connection.h"
#include "strings.h"
#include "config.h"
------snip---------
-----------------end file
----------------top of configfile.cpp
#include <iostream>
#include <fstream>
#include "configfile.h"
#include "strings.h"
-----snip------------
--------------end of file
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Pointers to classes
2004-06-20 4:46 Pointers to classes Eric
@ 2004-06-20 5:06 ` John T. Williams
2004-06-20 5:18 ` John T. Williams
0 siblings, 1 reply; 6+ messages in thread
From: John T. Williams @ 2004-06-20 5:06 UTC (permalink / raw)
To: eric; +Cc: linux-c-programming
Hello,
I'm only taking a stab here, to be sure I'd have to see the complete
file and #include's but it looks like your trying to use ConfigFile type
before you define it or prototype it. If you define class ConfigFile
somewhere else then you can probably fix the error by adding the line
class ConfigFile;
before you try to define
class Connection {
...
};
you might also want to add the lines
#ifndef CONNECT_H
#define CONNECT_H
to the very top of connect.h
and
#endif
to the very bottom of connect.h
and do the same thing for every .h file you create. This can save you
many many problems later It helps to prevent accidental infinite
includes and other common problems.
Just to sum up my revised version of you connection.h
---------------------start connection.h
#ifndef CONNECTION_H
#define CONNECTION_H
class ConfigFile;
class Connection
{
private:
...
ConfigFile* config;
...
public:
...
ConfigFile* getConfig();
};
#endif
---------------------end connection.h
On Sun, 2004-06-20 at 00:46, Eric wrote:
> Hello
> First I would like to say im fairly new to C++ and keep in mind you will
> probably see many glaring mistakes in reading my code, please be kind. Hey,
> thats how we learn :)
>
> My problem is with pointers to classes. I am writing a VPN management program
> and have a class Connection, which handles a connection ppp0, ppp1, etc, and
> is closely tied with a class ConfigFile, which handles all the variables for
> that connection obviously read from a file.
>
> I need to have a pointer to an object ConfigFile inside the Connection class
> so I can access variables and such associated with that connection that were
> read from a file. However, I am getting very strange compile errors when
> trying to define the pointer to a class. These are the errors. Below are the
> relevant files and how they are included.
>
> */home/bot403/programming/projects/vpn_client/src/connection.h:28: error:
> syntax error before `*' token
> */home/bot403/programming/projects/vpn_client/src/connection.h:31: error:
> parse error before `*' token
> */home/bot403/programming/projects/vpn_client/src/connection.h:36: error:
> syntax error before `*' token
>
>
> Here are the relevant files. Please let me know if you need additional
> information.
>
> BTW, I don't consider making Connection a sublass of configfile a solution
> becuase I am already teaching myself alot with this project and am happy with
> simpleton solutions. Subclasses/Inheritance would require that I teach
> myself way too much to accomplish way too little.
>
>
> ---------------------start connection.h
> class Connection
> {
> private:
> char ifName[ 5 ];
> char pppdopts[256];
> char sshopts[256];
> char pidfile[256];
> ConfigFile * config;
> pid_t pid;
> public:
> Connection(ConfigFile * cFile );
> void printName( void );
> int start();
> int stop();
> ~Connection();
> ConfigFile * getConfig();
>
> };
> ----------------------- end file
> -----------------------start configfile.h
> int readElement( char buffer[], char option[], char argument[] );
>
> using namespace std;
>
> class ConfigFile {
> private:
> ifstream configFile;
> char configFileName[ 256 ];
>
> //File variables
> char pppdevice[ 8 ];
> char vpnhost[ 256 ];
> char logfile[ 256 ];
> char *pppdopts;
> //SSH Specific
> char sshcipher[ 15 ];
> char sshlogin[ 256 ];
> char sshidentity[ 256 ];
> char *sshopts;
> char sshport[ 10 ];
> //Misc
> char delay[ 10 ];
> char vpnpidfile[80];
> //Connection Details
> char *networks[5];
> char *nameservers[2];
> //End File Variables
>
> public:
> ConfigFile( char *Name );
> ~ConfigFile();
> int readConfig();
> void printConfig();
> int writeConfig(char *Filename);
>
> char *getsshopts(char buffer[]);
> char *getpppdopts(char buffer[]);
> char *getlogfile(char buffer[]);
> char *getpidfile(char buffer[]);
>
> //FIXME
> //char *getnetworks();
> //char *getnameservers();
>
>
> };
>
> -----------end of file
> -----------top of connection.cpp
>
> #include <iostream>
> #include <fstream>
> #include <unistd.h>
> #include "configfile.h"
> #include "connection.h"
> #include "strings.h"
> #include "config.h"
> ------snip---------
>
> -----------------end file
>
> ----------------top of configfile.cpp
>
> #include <iostream>
> #include <fstream>
> #include "configfile.h"
> #include "strings.h"
> -----snip------------
>
> --------------end of file
> -
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Pointers to classes
2004-06-20 5:06 ` John T. Williams
@ 2004-06-20 5:18 ` John T. Williams
2004-06-20 12:49 ` LDB
2004-06-20 13:30 ` Eric
0 siblings, 2 replies; 6+ messages in thread
From: John T. Williams @ 2004-06-20 5:18 UTC (permalink / raw)
To: jtwilliams; +Cc: eric, linux-c-programming
Sorry wrong terminology. its not prototyping, but I can't remember what
you call it when you give a partial class, i.e.
class SomeClass;
rather then full class
class SomeClass {
private:
...
public:
...
};
?Maybe forward declaration ?
On Sun, 2004-06-20 at 01:06, John T. Williams wrote:
> Hello,
>
> I'm only taking a stab here, to be sure I'd have to see the complete
> file and #include's but it looks like your trying to use ConfigFile type
> before you define it or prototype it. If you define class ConfigFile
> somewhere else then you can probably fix the error by adding the line
>
> class ConfigFile;
>
> before you try to define
>
> class Connection {
> ...
> };
>
> you might also want to add the lines
>
> #ifndef CONNECT_H
> #define CONNECT_H
>
> to the very top of connect.h
>
> and
>
> #endif
>
> to the very bottom of connect.h
>
> and do the same thing for every .h file you create. This can save you
> many many problems later It helps to prevent accidental infinite
> includes and other common problems.
>
> Just to sum up my revised version of you connection.h
>
> ---------------------start connection.h
> #ifndef CONNECTION_H
> #define CONNECTION_H
>
> class ConfigFile;
>
> class Connection
> {
> private:
> ...
> ConfigFile* config;
> ...
> public:
> ...
> ConfigFile* getConfig();
> };
>
> #endif
> ---------------------end connection.h
>
>
>
> On Sun, 2004-06-20 at 00:46, Eric wrote:
> > Hello
> > First I would like to say im fairly new to C++ and keep in mind you will
> > probably see many glaring mistakes in reading my code, please be kind. Hey,
> > thats how we learn :)
> >
> > My problem is with pointers to classes. I am writing a VPN management program
> > and have a class Connection, which handles a connection ppp0, ppp1, etc, and
> > is closely tied with a class ConfigFile, which handles all the variables for
> > that connection obviously read from a file.
> >
> > I need to have a pointer to an object ConfigFile inside the Connection class
> > so I can access variables and such associated with that connection that were
> > read from a file. However, I am getting very strange compile errors when
> > trying to define the pointer to a class. These are the errors. Below are the
> > relevant files and how they are included.
> >
> > */home/bot403/programming/projects/vpn_client/src/connection.h:28: error:
> > syntax error before `*' token
> > */home/bot403/programming/projects/vpn_client/src/connection.h:31: error:
> > parse error before `*' token
> > */home/bot403/programming/projects/vpn_client/src/connection.h:36: error:
> > syntax error before `*' token
> >
> >
> > Here are the relevant files. Please let me know if you need additional
> > information.
> >
> > BTW, I don't consider making Connection a sublass of configfile a solution
> > becuase I am already teaching myself alot with this project and am happy with
> > simpleton solutions. Subclasses/Inheritance would require that I teach
> > myself way too much to accomplish way too little.
> >
> >
> > ---------------------start connection.h
> > class Connection
> > {
> > private:
> > char ifName[ 5 ];
> > char pppdopts[256];
> > char sshopts[256];
> > char pidfile[256];
> > ConfigFile * config;
> > pid_t pid;
> > public:
> > Connection(ConfigFile * cFile );
> > void printName( void );
> > int start();
> > int stop();
> > ~Connection();
> > ConfigFile * getConfig();
> >
> > };
> > ----------------------- end file
> > -----------------------start configfile.h
> > int readElement( char buffer[], char option[], char argument[] );
> >
> > using namespace std;
> >
> > class ConfigFile {
> > private:
> > ifstream configFile;
> > char configFileName[ 256 ];
> >
> > //File variables
> > char pppdevice[ 8 ];
> > char vpnhost[ 256 ];
> > char logfile[ 256 ];
> > char *pppdopts;
> > //SSH Specific
> > char sshcipher[ 15 ];
> > char sshlogin[ 256 ];
> > char sshidentity[ 256 ];
> > char *sshopts;
> > char sshport[ 10 ];
> > //Misc
> > char delay[ 10 ];
> > char vpnpidfile[80];
> > //Connection Details
> > char *networks[5];
> > char *nameservers[2];
> > //End File Variables
> >
> > public:
> > ConfigFile( char *Name );
> > ~ConfigFile();
> > int readConfig();
> > void printConfig();
> > int writeConfig(char *Filename);
> >
> > char *getsshopts(char buffer[]);
> > char *getpppdopts(char buffer[]);
> > char *getlogfile(char buffer[]);
> > char *getpidfile(char buffer[]);
> >
> > //FIXME
> > //char *getnetworks();
> > //char *getnameservers();
> >
> >
> > };
> >
> > -----------end of file
> > -----------top of connection.cpp
> >
> > #include <iostream>
> > #include <fstream>
> > #include <unistd.h>
> > #include "configfile.h"
> > #include "connection.h"
> > #include "strings.h"
> > #include "config.h"
> > ------snip---------
> >
> > -----------------end file
> >
> > ----------------top of configfile.cpp
> >
> > #include <iostream>
> > #include <fstream>
> > #include "configfile.h"
> > #include "strings.h"
> > -----snip------------
> >
> > --------------end of file
> > -
> > To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> > the body of a message to majordomo@vger.kernel.org
> > More majordomo info at http://vger.kernel.org/majordomo-info.html
>
> -
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Pointers to classes
2004-06-20 5:18 ` John T. Williams
@ 2004-06-20 12:49 ` LDB
2004-06-20 13:30 ` Eric
1 sibling, 0 replies; 6+ messages in thread
From: LDB @ 2004-06-20 12:49 UTC (permalink / raw)
To: jtwilliams; +Cc: eric, linux-c-programming
Abstract class .........
LDB
John T. Williams wrote:
>Sorry wrong terminology. its not prototyping, but I can't remember what
>you call it when you give a partial class, i.e.
>class SomeClass;
>
>rather then full class
>
>class SomeClass {
>private:
>...
>public:
>...
>};
>
>?Maybe forward declaration ?
>
>
>
>On Sun, 2004-06-20 at 01:06, John T. Williams wrote:
>
>
>>Hello,
>>
>>I'm only taking a stab here, to be sure I'd have to see the complete
>>file and #include's but it looks like your trying to use ConfigFile type
>>before you define it or prototype it. If you define class ConfigFile
>>somewhere else then you can probably fix the error by adding the line
>>
>>class ConfigFile;
>>
>>before you try to define
>>
>>class Connection {
>>...
>>};
>>
>>you might also want to add the lines
>>
>>#ifndef CONNECT_H
>>#define CONNECT_H
>>
>>to the very top of connect.h
>>
>>and
>>
>>#endif
>>
>>to the very bottom of connect.h
>>
>>and do the same thing for every .h file you create. This can save you
>>many many problems later It helps to prevent accidental infinite
>>includes and other common problems.
>>
>>Just to sum up my revised version of you connection.h
>>
>>---------------------start connection.h
>>#ifndef CONNECTION_H
>>#define CONNECTION_H
>>
>>class ConfigFile;
>>
>>class Connection
>>{
>>private:
>>...
>>ConfigFile* config;
>>...
>>public:
>>...
>>ConfigFile* getConfig();
>>};
>>
>>#endif
>>---------------------end connection.h
>>
>>
>>
>>On Sun, 2004-06-20 at 00:46, Eric wrote:
>>
>>
>>>Hello
>>> First I would like to say im fairly new to C++ and keep in mind you will
>>>probably see many glaring mistakes in reading my code, please be kind. Hey,
>>>thats how we learn :)
>>>
>>> My problem is with pointers to classes. I am writing a VPN management program
>>>and have a class Connection, which handles a connection ppp0, ppp1, etc, and
>>>is closely tied with a class ConfigFile, which handles all the variables for
>>>that connection obviously read from a file.
>>>
>>> I need to have a pointer to an object ConfigFile inside the Connection class
>>>so I can access variables and such associated with that connection that were
>>>read from a file. However, I am getting very strange compile errors when
>>>trying to define the pointer to a class. These are the errors. Below are the
>>>relevant files and how they are included.
>>>
>>>*/home/bot403/programming/projects/vpn_client/src/connection.h:28: error:
>>>syntax error before `*' token
>>>*/home/bot403/programming/projects/vpn_client/src/connection.h:31: error:
>>>parse error before `*' token
>>>*/home/bot403/programming/projects/vpn_client/src/connection.h:36: error:
>>>syntax error before `*' token
>>>
>>>
>>>Here are the relevant files. Please let me know if you need additional
>>>information.
>>>
>>>BTW, I don't consider making Connection a sublass of configfile a solution
>>>becuase I am already teaching myself alot with this project and am happy with
>>>simpleton solutions. Subclasses/Inheritance would require that I teach
>>>myself way too much to accomplish way too little.
>>>
>>>
>>>---------------------start connection.h
>>>class Connection
>>>{
>>>private:
>>> char ifName[ 5 ];
>>> char pppdopts[256];
>>> char sshopts[256];
>>> char pidfile[256];
>>> ConfigFile * config;
>>> pid_t pid;
>>>public:
>>> Connection(ConfigFile * cFile );
>>> void printName( void );
>>> int start();
>>> int stop();
>>> ~Connection();
>>> ConfigFile * getConfig();
>>>
>>>};
>>>----------------------- end file
>>>-----------------------start configfile.h
>>>int readElement( char buffer[], char option[], char argument[] );
>>>
>>>using namespace std;
>>>
>>>class ConfigFile {
>>> private:
>>> ifstream configFile;
>>> char configFileName[ 256 ];
>>>
>>> //File variables
>>> char pppdevice[ 8 ];
>>> char vpnhost[ 256 ];
>>> char logfile[ 256 ];
>>> char *pppdopts;
>>> //SSH Specific
>>> char sshcipher[ 15 ];
>>> char sshlogin[ 256 ];
>>> char sshidentity[ 256 ];
>>> char *sshopts;
>>> char sshport[ 10 ];
>>> //Misc
>>> char delay[ 10 ];
>>> char vpnpidfile[80];
>>> //Connection Details
>>> char *networks[5];
>>> char *nameservers[2];
>>> //End File Variables
>>>
>>> public:
>>> ConfigFile( char *Name );
>>> ~ConfigFile();
>>> int readConfig();
>>> void printConfig();
>>> int writeConfig(char *Filename);
>>>
>>> char *getsshopts(char buffer[]);
>>> char *getpppdopts(char buffer[]);
>>> char *getlogfile(char buffer[]);
>>> char *getpidfile(char buffer[]);
>>>
>>> //FIXME
>>> //char *getnetworks();
>>> //char *getnameservers();
>>>
>>>
>>>};
>>>
>>>-----------end of file
>>>-----------top of connection.cpp
>>>
>>>#include <iostream>
>>>#include <fstream>
>>>#include <unistd.h>
>>>#include "configfile.h"
>>>#include "connection.h"
>>>#include "strings.h"
>>>#include "config.h"
>>>------snip---------
>>>
>>>-----------------end file
>>>
>>>----------------top of configfile.cpp
>>>
>>>#include <iostream>
>>>#include <fstream>
>>>#include "configfile.h"
>>>#include "strings.h"
>>>-----snip------------
>>>
>>>--------------end of file
>>>-
>>>To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
>>>the body of a message to majordomo@vger.kernel.org
>>>More majordomo info at http://vger.kernel.org/majordomo-info.html
>>>
>>>
>>-
>>To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
>>the body of a message to majordomo@vger.kernel.org
>>More majordomo info at http://vger.kernel.org/majordomo-info.html
>>
>>
>
>-
>To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
>the body of a message to majordomo@vger.kernel.org
>More majordomo info at http://vger.kernel.org/majordomo-info.html
>
>
>
>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Pointers to classes
2004-06-20 5:18 ` John T. Williams
2004-06-20 12:49 ` LDB
@ 2004-06-20 13:30 ` Eric
1 sibling, 0 replies; 6+ messages in thread
From: Eric @ 2004-06-20 13:30 UTC (permalink / raw)
To: jtwilliams; +Cc: linux-c-programming
On Sunday 20 June 2004 12:18 am, John T. Williams wrote:
> Sorry wrong terminology. its not prototyping, but I can't remember what
> you call it when you give a partial class, i.e.
> class SomeClass;
>
> rather then full class
>
> class SomeClass {
> private:
> ...
> public:
> ...
> };
>
> ?Maybe forward declaration ?
The abstract class worked like a charm! I think I had some weird include
problems where they werent going in the order I needed them to and I couldn't
trace it down. Firthermore I didnt want to redefine the class inside
connection.h...that would just be silly. But an abstract class worked
beautifully.
> > you might also want to add the lines
> >
> > #ifndef CONNECT_H
> > #define CONNECT_H
> >
> > to the very top of connect.h
> >
> > and
> >
> > #endif
> >
> > to the very bottom of connect.h
Thats a great tip. Ill implement that from now on.
^ permalink raw reply [flat|nested] 6+ messages in thread
* RE: Pointers to classes
@ 2004-06-21 14:55 Huber, George K RDECOM CERDEC STCD SRI
0 siblings, 0 replies; 6+ messages in thread
From: Huber, George K RDECOM CERDEC STCD SRI @ 2004-06-21 14:55 UTC (permalink / raw)
To: linux-c-programming
Eric wrote:
> The abstract class worked like a charm! I think I had some weird include
>problems where they werent going in the order I needed them to and I
couldn't
>trace it down. Firthermore I didnt want to redefine the class inside
>connection.h...that would just be silly. But an abstract class worked
>beautifully.
Actually, the correct term, AFAIR, is a forward declaration.
An `abstract class' is a class that can not be instantiated due to having
one or
more abstract functions in the class. The `abstract class' is used as a
base class
for inheridence. For example, the following class is an abstract class:
class CShape
{
public:
void virtual Draw() = 0;
int GetType();
...
private:
int m_nType;
};
The strange looking syntax `void virtual Draw() = 0' is how an abstract
function is
declared. This has several consequences:
1. As stated before, it is impossible to instantiate an abstract class.
2. The compiler will require that _all_ classes derived from an abstract
class provide
an implementation for the abstract function.
3. You can use pointers to the abstract class to get polymorphic behavior.
For example,
if you represent a picture an a collections of shapes, you can declare a
picture like:
std::vector<CShape*> pic;
and the compiler will be happy. You will need typecast the derived
classes to the
parent class to insert into the collection.
George
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2004-06-21 14:55 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-06-20 4:46 Pointers to classes Eric
2004-06-20 5:06 ` John T. Williams
2004-06-20 5:18 ` John T. Williams
2004-06-20 12:49 ` LDB
2004-06-20 13:30 ` Eric
-- strict thread matches above, loose matches on Subject: below --
2004-06-21 14:55 Huber, George K RDECOM CERDEC STCD SRI
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).