c++ is not c.
By defining CServer, you define a new object type.
Your compiler has to know from both implementation files how the class =
is
defined.
The usual way to do this is putting the class definition in a header =
file,
and include it in both implementation files eg:
MyClass.h:
class MyClass{
MyClass();
void anyFn();
...
};
MyClass.cpp:
#include "MyClass.h"
MyClass::MyClass()
{}
void MyClass::anyFn()
{}
main.cpp
#include "MyClass.h"
main()
{
MyClass myclass;
myclass.anyFn();
}
> -----Original Message-----
> From: Wobien [mailto:wo...@pl...]
> Sent: Tuesday, January 07, 2003 11:38 PM
> To: dev-cpp-users-list
> Subject: Re: [Dev-C++] using 2 .c files
>=20
>=20
>=20
> ----- Original Message -----
> From: "Steve Bertrand" <iac...@no...>
> To: <dev...@li...>
> Sent: Tuesday, January 07, 2003 7:55 PM
> Subject: [Dev-C++] using 2 .c files
>=20
>=20
> > Hi there, this is my first post, so please forgive me if my=20
> message is
> > out of context for this list.
> >
> > I am building an application in c++ on freebsd, and can't=20
> get 2 .c files
> > to merge properly. When I compile, I get 'undefined CServer' when =
I
> > compile. I tried defining the 'server' variable with=20
> extern, but I don't
> > know if I was using extern in the proper context. For=20
> instance, here are
> > snippets of the 2 files:
> >
> Each file you make must have the definitions of all the=20
> objects you use in
> it.
> Your file main.cc uses the class CServer, but it does not=20
> have a definition
> of it.
> You can put the declaration of class CServer in a header=20
> file and include
> that header file in server.cc and in main.cc
>=20
> > -----snip server.cc--------
> >
> > #include "myheader.h"
> >
> > class CServer {
> > int var1;
> > int var2;
> > string var3;
> > string var4;
> >
> > void addServer(int, string, string);
> > }
> >
> > void CServer::addServer(int a, string b, string c) {
> >
> > var3 =3D c;
> > var4 =3D b;
> > var3 =3D a;
> > }
> >
> > --------end snip server.c-----------
> >
> > ------snip main.cc-----------
> >
> > void main() {
> >
> > CServer *server; // The problem is here.
> > server =3D new CServer;
> >
> > server.addServer(feedvar, feedvar, feedvar);
> >
> > /* Do some other stuff */
> >
> > ----------end snip main.cc---------
> >
> I do=B4nt understand exactly what your second problem is, but I=20
> think it might
> be that
> server.cc does not have the object db_connection you define in =
main.cc
> You can try to add in server.cc:
>=20
> extern Connection db_connection;
>=20
> before you work with the connection.
>=20
> Wobien
>=20
> > Another problem: In this same program, I would like main.cc=20
> to connect
> > to a mysql database early in main(), then allow the=20
> server.cc file (more
> > specifically, the member functions of CServer) to=20
> read/write to the db.
> > I know the mysql syntax is correct, as when I put all of=20
> the code in
> > one file, everything works ok. Is this another extern=20
> problem? What
> > part of the mysql code to I have to initialize with extern=20
> and where?
> > Example:
> >
> > main.c------
> >
> > // all of the mysql db stuff defined and included in 'myheader.h'
> > #include "myheader.h"
> >
> > main() {
> >
> > Connection db_connection;
> > db_connection.connect(//send db info here)
> >
> > end snip main.c----------
> >
> > Eventually, the server.c file opens up a table, then does=20
> it's thing,
> > but when compiling, it seems as though the database=20
> connection doesn't
> > even exist (from the point of view of server.c).
> >
> > What is the easiest way to link these 2 files so they merge=20
> and appear
> > as one? Or is that the purpose of extern? I simply want=20
> to divide up
> > my code so I can have 'modules' written in each .c file to make it
> > easier to maintain.
> >
> > Thanks for any help in advance.
> >
> > Steve Bertrand
> >
> >
> >
> >
> >
> > -------------------------------------------------------
> > This SF.NET email is sponsored by:
> > SourceForge Enterprise Edition + IBM + LinuxWorld =3D Something 2 =
See!
> > http://www.vasoftware.com
> > _______________________________________________
> > Dev-cpp-users mailing list
> > Dev...@li...
> > TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>=20
>=20
>=20
> -------------------------------------------------------
> This SF.NET email is sponsored by:
> SourceForge Enterprise Edition + IBM + LinuxWorld =3D Something 2 =
See!
> http://www.vasoftware.com
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>=20
|