just wanted to start some C++ and created these simple class:
Ellipsoid.h
#ifndef __Ellipsoid__Ellipsoid__
#define __Ellipsoid__Ellipsoid__
#include <iostream>
#include <cassert>
class Ellipsoid {
private:
double axisA;
double flatteningF;
public:
Ellipsoid() {};
Ellipsoid(double aIn, double fIn);
double getAxisA();
double getFlatteningF();
};
#endif /* defined(__Ellipsoid__Ellipsoid__) */
Ellipsoid.cpp
#include "Ellipsoid.h"
Ellipsoid::Ellipsoid (double aIn, double fIn) : axisA(aIn), flatteningF(fIn) {};
int main() {
std::cout << "bla";
Ellipsoid el = Ellipsoid(44.3, 32);
double test = el.getAxisA();
return 0;
}
as you can see nothing special here. i'm using xcode on osx10.8.
But when i run the programm i come to this error:
Undefined symbols for architecture x86_64:
"Ellipsoid::getAxisA()", referenced from:
_main in Ellipsoid.o
ld: symbol(s) not found for architecture x86_64
and i really can't figure out whats wrong. tried to set the architecture to 32 bit but this won't work neither
The definition of the Ellipsoid::getAxisA() function is missing. You must define somewhere. Right now you only have a declaration, not a definition. The definition could look something like this:
double Ellipsoid::getAxisA() { return axisA; }
And would live in Ellipsoid.cpp.
Related
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();
I have seen many related questions to this problem, but after carefully following advice from members, my problem still persists. The code is quite simple. I only have the following header file ("instrument.h"), which contains the base class and the template class:
#include <stdio.h>
#include <string>
using namespace std;
class Instrument
{
public:
Instrument();
virtual void print() const = 0;
};
template <class parameter> class Equity : public Instrument
{
public:
Equity();
virtual void print() const;
};
Now, in my main function on main.cpp I only do the following:
#include "instrument.h"
#include <iostream>
int main() {
Equity<double> pb;
return 0;
}
Well, I get the very well-known error:
Undefined symbols for architecture x86_64:
"Equity<double>::Equity()", referenced from:
_main in main.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 already changed in Build Settings the C++ standard library to libstdc++, also to default compiler, and so on. Do I have a problem with my project settings? Is perhaps the template wrongly implemented? I was thinking I should also have a instrument.cpp file, but then again definitions for templates must be kept in the header file so that would probably crash too.
Thanks in advance
You declared the default constructors for both Instrument and Equity but defined them nowhere.
Alter their definitions appropriately:
public:
Equity() = default; // Or {} in pre-C++11
// ^^^^^^^^^
(And equivalently for Instrument)
You can also completely omit the declarations of any default constructors for now since you didn't declare any other constructors in both Equity and Instrument and the default constructors will be generated automatically.
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.
I tried compiling a simple program on Xcode and got the following messages:
function<anonymous namespace>::Initialize' has internal linkage but is not defined
function<anonymous namespace>::HandleBadInput' has internal linkage but is not defined
Undefined symbols for architecture x86_64:
"(anonymous namespace)::Initialize()", referenced from:
_main in main.o
"(anonymous namespace)::HandleBadInput()", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The header file looks like this:
#ifndef WJKErrorHandling
#define WJKErrorHandling
namespace WJKErrorHandling{
void Initialize(void);
int HandleBadInput(void);
}
#endif // defined(WJKErrorHandling)
the implementation file looks like this:
#include <iostream>
#include "WJKErrorHandling.h"
namespace WJKErrorHandling{
void Initialize(void){
std::cin.exceptions(std::cin.failbit);
}
int HandleBadInput(void){
std::cerr << "Input Error: wrong type?\n";
std::cin.clear();
char BadInput[5];
std::cin >> BadInput;
return 1;
}
}
and main.cpp looks like this:
#include <iostream>
#include "WJKErrorHandling.h"
void Prompt (void){
//Prompts the user to begin entering numbers
std::cout << "Begin entering numbers: \n";
}
float GetNumber (void){
std::cout << "Number: \n";
float Number;
std::cin >> Number;
return Number;
}
std::string GetString (void){
std::cout << "String: \n";
std::string String;
std::cin >> String;
return String;
}
int main()
{
Prompt();
WJKErrorHandling::Initialize();
int ReturnCode = 0;
try{
float Number = GetNumber();
std::cout << Number;
std::string String = GetString();
std::cout << String;
std::cout << "SUCCESS!!!!\n";
}
catch(...){
ReturnCode = WJKErrorHandling::HandleBadInput();
}
return ReturnCode;
}
I've tried finding an answer so far, but I haven't understood any of the posts that I've found. I'm new with C++, so any help would be greatly appreciated!
Your #define Guard is causing name lookup issue.
change to below style should fix the issue:
#ifndef WJK_ERROR_HANDLING_H
#define WJK_ERROR_HANDLING_H
You could also use the non-standard but more idiomatic #pragma once, according to its wikipedia page it is supported by all major compilers.
Since many compilers have optimizations to identify include guards, there is no speed advantage between the two. For myself I see the following advantages of #pragma once:
It has only one meaning (whereas defines serve different purposes) and will not clash with other things (e.g. a namespace as in your case).
It is little to type and simple to remember.
You cannot have errors due to a typo (WJKERRORHANDLNG_H, ups and I is missing), because you started the header as a copy of another and forgot to change the include guard, which gives you rather nasty bughunting sessions.
This turns out to be a bad include guard:
#ifndef WJKErrorHandling
#define WJKErrorHandling
because you later try to use WJKErrorHandling as a namespace, but the macro makes it go away.
Change your include guard to something like:
#ifndef WJKERRORHANDLING_H
#define WJKERRORHANDLING_H
which is probably more idiomatic and less likely to conflict with something.
I have a noob question here.
I'm getting my head around the C++ structure and syntax and I've hit a bit of a wall.
I know I am missing something from my concept. So first a little code to help describe the situation.
Control.h
#pragma once
#ifndef CONTROL_H
#define CONTROL_H
class Control
{
public:
Control();
~Control();
private:
public:
};
#endif /*CONTROL_H*/
Control.cpp
#include "Control.h"
#include "Hello.h"
Hello helloObj;
Control::Control()
{
}
Control::~Control()
{
}
int main()
{
int a = helloObj.HelloWorld();
return 0;
}
Hello.h
#pragma once
#ifndef HELLO_H
#define HELLO_H
class Hello
{
public:
Hello();
~Hello();
private:
public:
int HelloWorld(void);
};
#endif /*HELLO_H*/
Hello.cpp
#include "Hello.h"
Hello::Hello()
{
}
Hello::~Hello()
{
}
int HelloWorld()
{
return 5;
}
I try and compile control.cpp with g++ on OSX 10.7 and get
Undefined symbols for architecture x86_64:
"Hello::Hello()", referenced from:
__static_initialization_and_destruction_0(int, int)in cccZHWtd.o
"Hello::~Hello()", referenced from:
___tcf_1 in cccZHWtd.o
"Hello::HelloWorld()", referenced from:
_main in cccZHWtd.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
Is it the compiler, my code or my concept of whats going on?
Am I not instantiating something correctly?
Any links describing this in more detail would be appreciated.
Ultimately I want to be able to run a function in another class and return the result...normal OO, keeping your program modular stuff....
The errors you are getting are Linking errors not compilation errors.
The linker is not able to find definitions of the said functions & hence it reports the errors. It seems You have not included the Hello.cpp file containing the function definitions in your project.
Make sure Hello.cpp is included in your project and is a part of your project or
If you are using command line for compilation and linking make sure you have specified Hello.cpp in the file names on the command line.
Most of the issue is me not being familiar as I should be with g++ (Thanks Als).
There were are few syntax issues as well (Thanks Brain).
Here is the corrected (albiet slightly bloated for an overview of stucture) code and g++ command
Control.h
#pragma once
#ifndef CONTROL_H
#define CONTROL_H
class CONTROL
{
private:
//nothing defined yet...
public:
Control(); //default constructor
~Control(); //default destructor
};
#endif /*CONTROL_H*/
Control.cpp
#include "Hello.h"
#include "Control.h"
Hello helloTest; //instantiates the Hello Object
Control::Control()
{
}
Control::~Control()
{
}
int main()
{
helloTest.HelloWorld();
return 0;
}
Hello.h
#pragma once
#ifndef HELLO_H
#define HELLO_H
class Hello
{
private:
//nothing defined yet
public:
Hello(); //default constructor
~Hello(); //default destructor
void HelloWorld();
};
#endif /*HELLO_H*/
Hello.cpp
#include "Hello.h"
#include <iostream> //so we can use 'cout'
using namespace std;
Hello::Hello()
{
}
Hello::~Hello()
{
}
void Hello::HelloWorld()
{
std::cout << "Hello lovelies!\n"; //The magic word.
}
Then we run g++ like so
g++ -o Hello ./Control.cpp ./Hello.cpp
g++ [option] [output file name] [input files]
First of all:
public:
Hello();
~Hello();
private:
public:
is pointless, a class defaults to private, and there is no need to make it
public twice nor do I know if you can do that furthermore if you have no private members private should not be in there (not trying to be mean just some advice :-) )
Now to answer the question (with a guess DISCLAIMER: I AM NOT 100% FAMILIAR WITH GCC):
This is a linker error, it may be there because
the compiler can not find the definition of
HelloWorld(void);.
Let me explain:
In your header file you wrote:
int HelloWorld(void);
However in your .cpp you write:
int HelloWorld()
{
return 5;
}
The function's (or in this case method because it is inside a class)
arguments need to be exactly the same in the header and source, you
can not even change the names (or at least you cant with VC++ which is
what I use; I have little experience with gcc) so this may be resolvable
by typing
int HelloWorld(void)
{
return 5;
}
Next (DISCLAIMER I AM NOT 100% familiar with the pre-proccsor):
You also use the #pragma once pre-proccsor tag, I dont use it but
I believe that means you can only include a file once and you have included Hello.h and Control.h twice, like I said I am no expert in the pre-proccsor but you commented out
HELLO_H
and
CONTROL_H