Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Okay.. so I need some help with my C++ class. I've been using tutorials online to help me, but now I'm just plain stuck. I have the following code:

server.h:

C++
#ifndef USER_H
#define USER_H
#include <vector>
#include <iostream>
#include <string>
#include <my_global.h>
#include <mysql.h>
namespace Mainframe
{
class User
{

public:

	Mainframe::User::User(std::string);
	int grabInfo();
};
}
#endif


server.cpp:

C++
#include "stdafx.h"
#include "server.h"

		MYSQL *conn;

    Mainframe::User::User(std::string id)
	{
	conn = mysql_init(NULL);

	//Connect to the server...
	mysql_real_connect(conn, "localhost", "root", "password", "users", 0, NULL, 0);
	}

	int Mainframe::User::grabInfo()
	{
		return mysql_query(conn, "SELECT * FROM users");
		mysql_close(conn);
	}


Now, as you may have noticed, I am using MySQL C API for database calling. This is my first time using it. Anyway, every time I try to compile I get the error:

error C2512: no appropriate default constructor available


So I did some research and put this before the constructor in my header file:

C++
Mainframe::User::User();


I then added the referenced function to my cpp file:

C++
Mainframe::User::User() { }


And I compile. The default constructor error is gone, but I'm instead left with this:

Error	5	error LNK2019: unresolved external symbol _mysql_real_connect@32 referenced in function "public: __thiscall Mainframe::User::User(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??0User@Mainframe@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)	E:\My Documents\Visual Studio 2010\Projects\LoveBird\MSQLClientTest\server.lib(connect.obj)	MSQLClientTest


Error	8	error LNK2019: unresolved external symbol _mysql_query@8 referenced in function "public: int __thiscall Mainframe::User::grabInfo(void)" (?grabInfo@User@Mainframe@@QAEHXZ)	E:\My Documents\Visual Studio 2010\Projects\LoveBird\MSQLClientTest\server.lib(connect.obj)	MSQLClientTest

Error	6	error LNK2019: unresolved external symbol _mysql_init@4 referenced in function "public: __thiscall Mainframe::User::User(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??0User@Mainframe@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)	E:\My Documents\Visual Studio 2010\Projects\LoveBird\MSQLClientTest\server.lib(connect.obj)	MSQLClientTest

Error	7	error LNK2019: unresolved external symbol _mysql_close@4 referenced in function "public: int __thiscall Mainframe::User::grabInfo(void)" (?grabInfo@User@Mainframe@@QAEHXZ)	E:\My Documents\Visual Studio 2010\Projects\LoveBird\MSQLClientTest\server.lib(connect.obj)	MSQLClientTest

Error	9	error LNK1120: 4 unresolved externals	1	E:\My Documents\Visual Studio 2010\Projects\LoveBird\Debug\MSQLClientTest.exe	1	MSQLClientTest


I have no idea what's wrong! Please help me. I'm pretty new to C++ and forgive me if this is a stupid question. Thanks in advance.
Posted
Updated 10-Mar-13 15:45pm
v2
Comments
Sergey Alexandrovich Kryukov 10-Mar-13 19:35pm    
Sorry, not a very good question, as well as the way of sorting things out. Here is why: compilation errors always come with file and line number. You need to show us what line is that. Comment it. "Unresolved external symbol" comes with symbol itself. You need to tell it. You need to read all error messages with more attention, to actually use them, and to ask better questions. Please do it, and use "Improve question" above.
—SA
Albert Holguin 10-Mar-13 19:48pm    
He's probably just forgetting to include the library for the linker.
Sergey Alexandrovich Kryukov 10-Mar-13 20:07pm    
Probably. Why would... oh, you already answered... let me see...
—SA

1 solution

When you use an external library, you have to include the lib file and dll (if dynamic, lib only if static). The header simply has the definitions but the compiled functions are inside the library file which is needed by the linker.

See here:
http://dev.mysql.com/doc/refman/5.0/en/building-clients.html[^]

FYI... your code is not great C++ code, perhaps you need to see the differences between namespaces and classes and see what's appropriate to use and when.

Also...
MYSQL *conn; //Create a new instance of the server

that comment is not right... that's simply defining a pointer, not creating an instance of anything.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 10-Mar-13 20:07pm    
Sure, a 5.
—SA
RadXPictures 10-Mar-13 21:48pm    
Sorry bout the comment... I come from a .NET background so some of my terminology may be off. Anyway, the thing is, I don't get these errors when I don't implement a default class constructor and they didn't exist before I tried creating a class. Which I don't understand.. maybe you could shed some light on. Also, I can't find the linker properties in the configuration properties, most likely because I am trying to create a *.lib file with this class. So not sure what to do.
Albert Holguin 10-Mar-13 21:50pm    
The reason they weren't there before is because your code wasn't even making it to that stage. It has to compile in order to get to the linker, it wasn't even compiling before.
RadXPictures 10-Mar-13 21:55pm    
Just something to consider.. as stated above.. I can't add *.lib files to my project because my project is attempting to generate a *.lib file. There are no linker properties in the properties box.
Albert Holguin 10-Mar-13 21:58pm    
Even when creating libs you have to specify the libs that are being used. They won't be compiled into your lib but u still have to do it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900