Linker undefined symbols error - c++

It seems like my two files, userinterface.h
#ifndef USERINTERFACE_H
#define USERINTERFACE_H
#include <string>
#include "vocabcollection.h"
namespace user_interface
{
//Finds a file
//
//Returns when user selects a file
std::string findFile();
//more comments followed by functions
}
#endif
and userinterface.cpp,
#include "userinterface.h"
using namespace std;
using namespace user_interface;
string findFile()
{
return "./";
}
//more placeholder implementations of such functions; void functions have nothing within
//the brackets
are giving me this slew of errors from the linker:
Undefined symbols for architecture x86_64:
make: Leaving directory `longdirectorypath'
"user_interface::showTestResults(int, int)", referenced from:
vocabCollection::test() in vocabcollection.o
"user_interface::get(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)", referenced from:
addNewCollection() in mainlogic.o
loadNewCollection() in mainlogic.o
"user_interface::findFile()", referenced from:
loadNewCollection() in mainlogic.o
"user_interface::displayMainMenu(std::vector<vocabCollection, std::allocator<vocabCollection> >)", referenced from:
mainlogic() in mainlogic.o
"user_interface::getUserAction()", referenced from:
mainlogic() in mainlogic.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [cheapassVocab.app/Contents/MacOS/cheapassVocab] Error 1
The process "/usr/bin/make" exited with code 2.
Error while building project cheapassVocab (target: Desktop)
When executing build step 'Make'
What's happening here?

In the header file, you declare the function findFile in the namespace user_interface. In the cpp file the free function findFile is defined. Yes, you are using namespace user_interface, but the compiler doesn't know that the findFile defined there belongs to namespace user_interface. The result of all this is that you've declared user_interface::findFile and defined ::findFile. When you call user_interface::findFile, the linker cannot find it, since there's only the free function findFile.
Easily solved - cpp file:
#include "userinterface.h"
using namespace std;
namespace user_interface
{
string findFile()
{
return "./";
}
}

You cannot implement findFile like that; it really has to go in the namespace:
namespace user_interface
{
string findFile()
{
return "./";
}
}
or:
string user_interface::findFile()
{
return "./";
}
The using directive is only for lookup, not for definitions - imagine what using namespace std; would do to all your function definitions otherwise!

You are defining findFile in the wrong namespace.
Either
std::string user_interface::findFile()
{
return "./";
}
or
namespace user_interface
{
std::string findFile()
{
return "./";
}
}
using does not affect where names are defined, it only affects how names are looked up.

Related

Not Compiling When Using Pointer to Function

My code compiles and runs fine when I incorporate all of this as a single file. However, when I use a header file and use separate files, I get this error:
Undefined symbols for architecture x86_64:
"someClass::newNode()", referenced from:
_main in check.o
someClass::insert(someClass::Node*, char const*, char const*) in entry.o
ld: sym
bol(s) not found for architecture x86_64
I have tried everything and I cannot find what the issue is. They compile separately using "-c" but linking the object files gives me the error. Also, I am using inclusion guards and all the suggested tips when including header files. Any help would be appreciated!
//.h file
class someClass{
public:
//other stuff
struct Node
{
//...
};
Node *newNode();
};
//entry.C
Node someClass::newNode(){
someClass::Node *bNode = new someClass::Node;
//...
return bNode;
}
//check.C
int main(){
//...
someClass obj;
someClass.Node *root = obj.newNode();
return 0;
}
For getting nested type in c++ use "::" instead of "."
someClass::Node *root = obj.newNode();

.txt function not linking to main function [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 6 years ago.
I am making a program with a list of baby names but I've decided to make a seperate function to open the file, this is what I have got so far.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void open_file(ifstream& in, char fileName[]);
void find_name(ifstream& in, string name, int numNames);
int main() {
const int NUMNAMES = 1000;
ifstream inStream;
char fileName[30];
string name;
cout << "Enter the name of the file that contains the names: " << endl;
open_file(inStream, fileName);
cout << "Enter the name to search for (capitalize first letter): " << endl;
cin >> name;
find_name(inStream, name, NUMNAMES);
inStream.close();
}
void open_file(ifstream& ) {
string line;
ifstream myfile ("babyNames.txt");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
cout << line << '\n';
}
myfile.close();
}
else cout << "I/O failure opening file babyNames";
}
Does anyone know why I am getting so many error messages:
Undefined symbols for architecture x86_64:
"find_name(std::__1::basic_ifstream<char, std::__1::char_traits<char> >&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, int)", referenced from:
_main in Untitled-1b6d2e.o
"open_file(std::__1::basic_ifstream<char, std::__1::char_traits<char> >&, char*)", referenced from:
_main in Untitled-1b6d2e.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Does anyone know what I am doing wrong, I feel like it is relatively close I'm just fairly new to streams in c++.
The shown code declares and calls the following functions:
void open_file(ifstream& in, char fileName[]);
void find_name(ifstream& in, string name, int numNames);
Unfortunately, the shown code does not define any of these two functions, and the two linking errors are the result of that.
The shown code does define some function that's also called open_file(), but it's a completely different function because it takes different parameters. The shown code does not define any function called find_name().
You cannot simply declare a function like:
void open_file(ifstream& in, char fileName[]);
And then expect the code for this function to automatically appear somewhere. You have to define, and write the contents of this function. The parameters in this function, when you define it, must be the same as what you declared here.

GCC Undefined symbols for architecture x86_64 in C++ Constructor

I just started up a new project, and my class skeleton does not compile. The compiler error I am receiving is:
Undefined symbols for architecture x86_64:
"SQLComm::ip", referenced from:
SQLComm::SQLComm(int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in SQLComm.o
"SQLComm::port", referenced from:
SQLComm::SQLComm(int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in SQLComm.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I have no idea why my code does not compile... Here's the class which errors:
SQLComm.h:
#ifndef __WhisperServer__SQLComm__
#define __WhisperServer__SQLComm__
#include <iostream>
#include <string>
class SQLComm {
public:
//Local vars
static int port;
static std::string ip;
//Public functions
void connect();
SQLComm(int sqlport, std::string sqlip);
~SQLComm();
private:
};
#endif /* defined(__WhisperServer__SQLComm__) */
And here's the SQLComm.cpp:
#include "SQLComm.h"
SQLComm::SQLComm(int sqlport, std::string sqlip){
ip = sqlip;
port = sqlport;
}
SQLComm::~SQLComm(){
}
void SQLComm::connect(){
}
The system is OSX10.9, and the compiler is GCC (in xCode).
If anyone could tell me why I am getting this error, I'd be very happy. Thanks in advance! :)
You have declared static variables but you haven't defined them. You need to add this
int SQLComm::port;
std::string SQLComm::ip;
to your SQLComm.cpp file.
Although... thinking about it this is probably not what you intended. You intended to declare non-static member variables, e.g., each instance of SQLComm should contain those variables, right? In that case, simply drop the static (and don't add the above to your .cpp file.
You need to define your static class variables. Try
int SQLComm::port;
std::string SQLComm::ip;
in SQLComm.cpp.
Note: Most probably, you do not want to declare both variable as static class variables but as normal instance variables.

What is the syntax for object instantiation in C++?

when I compile the following c++ code:
#include "ConstantList.h"
using namespace std;
int main() {
ConstantList* cl = new ConstantList();
//do something with cl
delete cl;
cl = NULL;
return 0;
}
The compiler gives me the error:
Undefined symbols:
"ConstantList::~ConstantList()", referenced from:
_main in ccNfeeDU.o
"ConstantList::ConstantList()", referenced from:
_main in ccNfeeDU.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
Am I not getting the syntax right for instantiating an object?
My ConstantList.h file looks like this:
#ifndef ConstantList_h
#define ConstantList_h
#include <string>
#include "Token.h"
using namespace std;
class ConstantListTail;
class ConstantList {
public:
ConstantList();
~ConstantList();
std::string toString();
void push_back(Token*);
void push_back(ConstantListTail*);
private:
Token* termString;
ConstantListTail* constantListTail;
};
#endif
Any help is greatly appreciated!
Your syntax is correct, because you are getting a linker error, not a compiler error. This error means that you are compiling your main without the source of the ConstantList.cpp, or linking without a reference to ConstantList.o
Compiling with this command should fix the error:
g++ collect2.cpp ConstantList.cpp
(I am assuming that the file with your main function is called collect2.cpp).
"undefined symbol" means you have declared the identifier (in this case the destructor), and it's used, but as far as the linker knows you have not defined it
add a definition somewhere, and make sure the compiled version is in one of the files the linker links
re "syntax for instantiation", unfortunately there is no dedicated syntax for that in C++
instead the functional cast notation is used for constructor invocations
perhaps the closest you get to a pure instantiation syntax is the new expression
re
using namespace std;
in a header file: don't.
for example, the standard library defines something called distance. what are the chances that some code that includes the header will have its own distance, and get a name collision? much higher than zero.
this doesn't mean you should never have using namespace std; in a header file, but you should never have it in the global namespace in a header file. and for other namespaces, be very aware of what that does, namely offering all the standard library names as part of also that namespace.

C++ Interface Compiling

EDIT:
I figured out the solution. I was not adding -combine to my compile instructions and that was generating the errors.
I'm in the process of working through the Deitel and Deitel book C++ How to Program and have hit a problem with building and compiling a C++ interface using g++. The problem is, I've declared the class in the .h file and defined the implementation in the .cpp file but I can't figure out how to get it to compile and work when I try to compile the test file I wrote. The g++ error I'm receiving is:
Undefined symbols:
"GradeBook::GradeBook(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)", referenced from:
_main in ccohy7fS.o
_main in ccohy7fS.o
"GradeBook::getCourseName()", referenced from:
_main in ccohy7fS.o
_main in ccohy7fS.o
ld: symbol(s) not found
collect2: ld returned 1 exit status<
If someone could point me in the right direction I'd be appreciative.
My header file:
//Gradebook 6 Header
//Purpose is to be the class declaration for the class Gradebook 6
//Declare public, privates, and function names.
#include //the standard c++ string class library
using std::string;
//define the class gradebook
class GradeBook
{
public: //all the public functions in the class
GradeBook(string ); //constructor expects string input
void setCourseName (string ); //method sets course name--needs string input
string getCourseName(); //function returns a string value
void displayMessage(); //to console
private: //all private members of the class
string courseName;
}; //ends the class declaration
My .cpp file is:
//Gradebook 6
// The actual implementation of the class delcaration in gradebook6.h
#include
using std::cout;
using std::endl;
#include "gradebook6.h" //include the class definition
//define the class gradebook
GradeBook::GradeBook(string name) //constructor expects string input
{
setCourseName(name); //call the set method and pass the input from the constructor.
}
void GradeBook::setCourseName (string name) //method sets course name--needs string input
{
courseName = name; //sets the private variable courseName to the value passed by name
}
string GradeBook::getCourseName() //function returns a string value
{
return courseName;
}
void GradeBook::displayMessage() //function does not return anything but displays //message to console
{
cout //message here, the pre tag isn't letting it display
} //end function displayMessage
Finally, the test file I wrote to implement the interface and test it.
// Gradebook6 Test
// Program's purpose is to test our GradeBook5 header file and file seperated classes
#include
using std::cout;
using std::endl;
#include "gradebook6.h" //including our gradebook header from the local file.
//being program
int main()
{
//create two gradebook objects
GradeBook myGradeBook1 ("CSC 101 Intro to C++ Programming"); //create a default object using the default constructor
GradeBook myGradeBook2 ("CSC 102 Data Structures in C++");
//display intitial course name
cout //another output message here that the code tag does not like
return 0;
}
Looks like you just need to link in the GradeBook.cpp object file to your final executable. Care to post your makefile or the way you are building it?
What you are seeing are linker errors (the indication is in the "ld returned" bit: ld is the linker). You need to either give all the .cpp files to g++, so it will compile and link them, or use the latter's -c switch to make a .o from the .cpp and then use THAT .o on a further g++ command to build (link) the executables.