Protected declaration

M

mkarja

Hi,

I have file, let's say file2.cpp that #includes file1.h.
file1.h looks something like this. Somewhat stripped version.
---------------------------------------------
#include <mysql/mysql.h>

class file1
{
public:
file1();
~file1();
protected:
MYSQL mysql;
}
---------------------------------------------
In file2.cpp I have a line: mysql_autocommit(&mysql, 0);

The g++ gives an error: error: 'mysql' was not declared in this scope.

Shouldn't I be able to use mysql in this case from the file2.cpp
because
mysql was declared in file1.h under Protected.

Thanks for all the help. I'm a bit lost with this.
 
M

Michael DOUBEZ

mkarja a écrit :
Hi,

I have file, let's say file2.cpp that #includes file1.h.
file1.h looks something like this. Somewhat stripped version.
---------------------------------------------
#include <mysql/mysql.h>

class file1
{
public:
file1();
~file1();
protected:
MYSQL mysql;
}
---------------------------------------------
In file2.cpp I have a line: mysql_autocommit(&mysql, 0);

The g++ gives an error: error: 'mysql' was not declared in this scope.

Shouldn't I be able to use mysql in this case from the file2.cpp
because
mysql was declared in file1.h under Protected.

I guess MYSQL is an opaque type. It means it is not defined in the
header only declared (like FILE in stdio).
You cannot instanciate it directly but use a pointer instead.

Somathing like:
Mysql* mysql=mysql_init();

Michael
 
M

Markus Moll

Hi

Michael said:
mkarja a écrit :

I guess MYSQL is an opaque type. It means it is not defined in the
header only declared (like FILE in stdio).
You cannot instanciate it directly but use a pointer instead.

The compiler is complaining about "mysql" not being declared, not
about "MYSQL" not being defined. While it's true that an undefined type
might result in the described error message (as a consequence and among
others), I don't think it's natural to assume that this is what in fact has
happened.

mkarja, could you please post file2.cpp or, if this is too long, a minimized
version that still produces the same error? Otherwise we can only tell you
that mysql was not declared in the scope in which you are using it.

It almost certainly has nothing to do with mysql being protected (then the
compiler would know it's declared, but would complain that it is not
accessible).

Markus
 
M

mkarja

mkarja a écrit :








I guess MYSQL is an opaque type. It means it is not defined in the
header only declared (like FILE in stdio).
You cannot instanciate it directly but use a pointer instead.

Somathing like:
Mysql* mysql=mysql_init();

Michael

Thanks for the answer.
I tried to put the: Mysql* mysql=mysql_init();
in file1.h under Protected, but it didn't help. The same error still.
 
M

Michael DOUBEZ

mkarja a écrit :
Thanks for the answer.
I tried to put the: Mysql* mysql=mysql_init();
in file1.h under Protected, but it didn't help. The same error still.

As Markus Moll said in another message, I have been a little quick with
the diagnostic. The only other reason I see for now is that you are not
in the file1 scope.

Your Contructor should be something like:
file1::file1()
{
mysql=mysql_init();
//I guess autocommit is a mode
mysql_autocommit(mysql, 0);
}

Michael
 
M

mkarja

Thanks for the answer.
I tried to put the: Mysql* mysql=mysql_init();
in file1.h under Protected, but it didn't help. The same error still.

Sorry Markus, didn't see your message until I had posted.
There's really nothing spectacular about file2.cpp, but I'll post
stripped
version of it here so you can mayby see why it gives this error.
There's only some if, for etc. stuff in it also, but they are after
that
mysql_autocommit(&mysql, 0); line.

-----------------------------
#include "file1.h"

#include "Poco/Exception.h"
#include "Poco/Util/Application.h"
#include <string>

using Poco::Util::Application;

file2::file2():
_Id(0),
_GroupId(0),
_userId(0)
{
}

file2::~file2()
{
}

int file2::saveToDatabase()
{
int is_client=0;

mysql_autocommit(&mysql, 0);

if (strcmp(getOwner().c_str(),"CLIENT")==0)
{
is_client=1;
}
else
{
Application::instance().logger().debug("Coming server ");
}
}
 
M

Markus Moll

Hi
Sorry Markus, didn't see your message until I had posted.
There's really nothing spectacular about file2.cpp, but I'll post
stripped
version of it here so you can mayby see why it gives this error.

You are defining:
file2::file2():
file2::~file2()
int file2::saveToDatabase()

From your previous posting:
class file1
{
public:
file1();
~file1();
protected:
MYSQL mysql;
}

(btw: there's a semi-colon missing at the end of the definition of file1)

So what is file2?
If it was meant to be file1, then why is there no declaration of _Id,
_GroupId, _userId and saveToDatabase() in file1? Is file2 derived from
file1?

Also, you are not allowed to use the identifiers _Id and _GroupId, as you
include <string>. Identifiers starting with _ + uppercase letter or
containing __ are reserved to the implementation whenever you use the
standard library. (Identifiers starting with _ + lowercase letter are also
reserved, but only in the global namespace.)

Markus
 
M

Michael DOUBEZ

mkarja a écrit :
[snip]
-----------------------------
#include "file1.h"

#include "Poco/Exception.h"
#include "Poco/Util/Application.h"
#include <string>

using Poco::Util::Application;

file2::file2():
_Id(0),
_GroupId(0),
_userId(0)
{
}

file2::~file2()
{
}

int file2::saveToDatabase()
{
int is_client=0;

mysql_autocommit(&mysql, 0);

if (strcmp(getOwner().c_str(),"CLIENT")==0)
{
is_client=1;
}
else
{
Application::instance().logger().debug("Coming server ");
}
}

It is ead easy then.
You use "clase file1" in the header and "class file2" in the source.

Michael
 
M

mkarja

Hi


You are defining:


From your previous posting:


(btw: there's a semi-colon missing at the end of the definition of file1)

So what is file2?
If it was meant to be file1, then why is there no declaration of _Id,
_GroupId, _userId and saveToDatabase() in file1? Is file2 derived from
file1?

Also, you are not allowed to use the identifiers _Id and _GroupId, as you
include <string>. Identifiers starting with _ + uppercase letter or
containing __ are reserved to the implementation whenever you use the
standard library. (Identifiers starting with _ + lowercase letter are also
reserved, but only in the global namespace.)

Markus

Thanks again, the semicolon is lost in copy/paste operation.
file2 is derived from file1. Also lost in copy/paste is the #include
"file2.h".
It compiles fine if I put the MYSQL mysql; in the file2.cpp.
Those _Id etc. are defined in file2.h.
 
R

red floyd

mkarja said:
Those _Id etc. are defined in file2.h.

Doesn't matter where they are defined. Any identifier with a leading
underscore, followed by an upper case letter is reserved to the
implementation in all circumstances. See 17.4.3.1.2
 
M

Markus Moll

Hi

red said:
No, such identifiers are reserved to the implementation. Period.

See 17.4.3.1.2

I did. But I have to disappoint you, 17.4.3 begins with:
"This subclause describes restrictions on C++ programs that use the
facilities of the C++ Standard Library."

Markus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,609
Members
45,253
Latest member
BlytheFant

Latest Threads

Top