How to initialize class with pointer to class
-
How to initialize class with pointer to class where object to is located;
For example Inside class A is created class B object, now class A can com to class B via object but for class B to com to class A would it be possible to initialize class with pointer or there are better solutions?
´´´
class FileCOM : public QObject
{public:
FileCOM(TCP_Socketptr); <<<------ this attempt fails and compiler returns : error: expected ')' before '' tokenvoid set_file_location(); void read_file_to_buffer(); void process_buffer(); void write_file(QByteArray data); std::vector <QByteArray> read_buffer;
};
´´´ -
just create a constructor that accepts such pointer ?
class FileCOM : public QObject
{
TCP_Socket *mykeptpointer;
public:
FileCOM(TCP_Socket * theptr) : mykeptpointer(theptr) {}
...
or do i misunderstand you ? -
Yes included , problem was due to double include in 2 files.
Is it possible to include file only for .h inside use so when other files include same .h part of files would not be double included?This post is deleted! -
Yes included , problem was due to double include in 2 files.
Is it possible to include file only for .h inside use so when other files include same .h part of files would not be double included? -
For example when include file1.h in file2.h and file2.h in file1.h compiler returns error: expected ')' before '' token FileCOM(TCP_SocketSOCKETptr); or mostly error: 'FileCOM' does not name a type
For pointer of class A in class B it need include class A .h in B .h file, but for class A to create object of B in .h file it need include of B .h file
-
For example when include file1.h in file2.h and file2.h in file1.h compiler returns error: expected ')' before '' token FileCOM(TCP_SocketSOCKETptr); or mostly error: 'FileCOM' does not name a type
For pointer of class A in class B it need include class A .h in B .h file, but for class A to create object of B in .h file it need include of B .h file
Ah, that is called circular include and often solved by using type forwards.
https://en.wikipedia.org/wiki/Circular_dependencylike
in your case
class TCP_Socket; //forward declarationclass FileCOM : public QObject
{
TCP_Socket *mykeptpointer;
....and then DONT include its .h file
-
Then it is not possible to declare class FileCOM in .h file of TCP_Socket only .cpp file
Thanks for your time and help, problem is there are multiple instances of sockets and each one should have fileCOM -
@Q139
well we all have :)Well in your case:
using external.then in mainwindow, u include TCP_Socket.H
then you have the instance to give to the other objects when you create them
-
Yes same class but different filenames.
Foward declaration is good solution and compiles , if it work good , appears to be complex solution for easy problem..h file
class TCP_Socket;
class FileCOM : public QObject
{
public:
TCP_Socket * SOCKETptr;
FileCOM(TCP_Socket * ptr){
SOCKETptr=ptr;
}.h file
class TCP_Socket : public QObject
{
public:
FileCOM * FILE;.cpp file
void TCP_Server_File::NewConnection()
{
TCP_Socket *soc = new TCP_Socket;
soc->FILE = new FileCOM(soc); <--- if working then here it should pointer of TCP_Socket class to FileCOM class.
soc->New_socket (server->nextPendingConnection ());
mainwin_ptr->AskForNewConnection (soc);
Client_count++;
} -
it seems fine.
SOCKETptr should be the object u expect.