I am attempting to connect to Sql Server with c++ using sqlapi++. I have gotten my code to work properly in my main function and when I am not instantiating an object within a class, but when I try to move my code over to a separate class I get errors.
The errors I'm getting are:
Expected a type specifier
and
syntax error: '&'
The code that produces the error is:
#include <string>
#include <SQLAPI.h>
class DbConnection
{
SAConnection con;
SACommand cmd(&con); //this line throws the error
public:
int age, id;
void Connect()
{
con.Connect(
"databaseName",
"userName",
"password",
SA_SQLServer_Client);
}
void Retrieve()
{
cmd.setCommandText("SELECT * FROM [dbo].[TableName]");
}
};
When I move the SAConnection
and SACommand
objects outside of the class DbConnection
, the errors go away and it seems to work.
#include <string>
#include <SQLAPI.h>
SAConnection con;
SACommand cmd(&con);
class DbConnection
{
//remaining code
};
I am having a difficult time understanding why that is, coming from other languages where I can instantiate and declare an object inside of a class is normal. I am missing some information, any explanation would be helpful.
Why can I not declare a class inside of another class?