C++ with multiple sources

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rottmanj
    New Member
    • Jan 2008
    • 46

    C++ with multiple sources

    I am working on better/re-educating myself in the ways of oop c++. One issue that I have run into is building an application with multiple class source files.

    Right now I am working on a simple application that connects to a mysql database and outputs information from the table. The one issue that I have run into is that when I try to compile I get the errors below.

    Code:
    undefined reference to `DbConnect::db_set(char const*)'
    undefined reference to `DbConnect::db_get()'
    I have a feeling it is two possible issues, one would be my linking and the other would be how I wrote the classes. Any help with this would be greatly appreciated.

    Here is my current implementation of my class files.

    DbConnect.h
    Code:
    #ifndef DBCONNECT_H_
    #define DBCONNECT_H_
    
    class DbConnect
    {
    public:
    	const char* keep_value;				// private data value
    	void db_set(const char* db_value);	// public method to set value
    	char db_get(void);					// public method to get value
    };
    
    #endif /*DBCONNECT_H_*/
    DbConnect.cpp
    Code:
    #include "DbConnect.h"
    
    const char* keep_value;
    void db_set(const char* db_value){
    	keep_value = db_value;
    }
    
    const char* db_get(){
    	return (keep_value);
    }
    dbTest.cpp (app application)
    Code:
    #include <iostream>
    #include "DbConnect.h"
     
    using namespace std;
    
    	 
    	int main(){
    		DbConnect hostname, datasource, username, password;
    		
    		hostname.db_set("test");
    		datasource.db_set("test");
    		username.db_set("test");
    		password.db_set("test");
    		
    		cout<<"Accessing data using class\n";
    		 cout<<"-------------------------\n";
    		 cout<<"Data value for hostname is "<<hostname.db_get()<<"\n";
    		 cout<<"Data value for datasource is "<<datasource.db_get()<<"\n";
    		 cout<<"Data value for username is "<<username.db_get()<<"\n";
    		 cout<<"Data value for password is "<<password.db_get()<<"\n";
    		 
    		 return 1;
    	}

    And here is my current implementation of my makefile

    Code:
    CXXFLAGS =	-O2 -g -Wall -fmessage-length=0
    
    OBJS =		dbTest.o DbConnect.o
    
    LIBS =
    
    TARGET =	dbTest
    
    $(TARGET):	$(OBJS)
    	$(CXX) -o $(TARGET) $(OBJS) $(LIBS)
    
    all:	$(TARGET)
    
    clean:
    	rm -f $(OBJS) $(TARGET)
  • boxfish
    Recognized Expert Contributor
    • Mar 2008
    • 469

    #2
    Code:
    const char* keep_value;
    Tke this line out of DbConnect.cpp. The only thing you need to do in the DbConnect.cpp is define the functions declared in the header file, so it's not necessary to declare the variables again.

    You need to prefix the functions in the .cpp file with the class name and scope resolution operator so the compiler knows which class they belong to. So it would be:
    Code:
    void DbConnect::db_set(const char* db_value){ 
        keep_value = db_value; 
    } 
      
    const char* DbConnect::db_get(){ 
        return (keep_value); 
    }
    I hope this is helpful.

    Comment

    Working...