Vscode terminal message - c++

I use mac vscode, and try to run the following file. When the error message pops up, the phrases are partially strange and not meaningful. Is there a way to change setting of vscode so that the output of my error message becomes normal again, something like Parent.class, Parent.h ... instead of Zn3_Parent...?
Below is the code I run:
#include <iostream>
using namespace std;
class Parent {
public :
virtual int func () = 0;
virtual ~Parent();
};
class Child : public Parent {
public :
int data;
Child (int k) {
data = k;
}
int func() { // virtual function
cout<<"Returning square of 10\n";
return 10*10;
}
void Display () {
cout<<data<<"\n";
}
~ Child() {
cout<<"Overridden Parents Destructor \n";
}
};
int main() {
Child a(10);
a.Display();
//cout << "asdf";
return 1;
}
And the output I get:
Undefined symbols for architecture x86_64:
"__ZN6ParentD2Ev", referenced from:
__ZN5ChildD1Ev in ccn2pmId.o
"__ZTI6Parent", referenced from:
__ZTI5Child in ccn2pmId.o
"__ZTV6Parent", referenced from:
__ZN6ParentC2Ev in ccn2pmId.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
I don't want those things like __ZN6, ZTI6, ZTV6.

Related

vtable C++ error after implementing pure virtual method

I have a pure virtual function defined in a class as below:
template <typename T>
class PositioningMethod {
public:
virtual ApproximatePosition *getPosition(std::list<T*> &observedRadioSignals) = 0;
};
and implementing it in ParticleFilter as below:
class ParticleFilter:public PositioningMethod<T> {
public:
virtual ApproximatePosition *getPosition(std::list<T*> &observedRadioSignals) {
/*Some code and return*/
return ApproximatePosition::from(xxxx, xxxx, xxxx());
}
};
but getting below errors:
"ParticleFilter<KnownBluetoothBeacon<CartesianLocation>, RadioProximity<BluetoothBeacon>
>::getPosition(std::__1::list<RadioProximity<BluetoothBeacon>*, std::__1::allocator<RadioProximity<BluetoothBeacon>*> >&)", referenced from:
vtable for RadioProximityParticleFilter in lib.a(RadioProximityParticleFilter.o)
"ParticleFilter<KnownBluetoothBeacon<CartesianLocation>, RadioProximity<BluetoothBeacon>
>::ParticleFilter(std::__1::list<KnownBluetoothBeacon<CartesianLocation>, std::__1::allocator<KnownBluetoothBeacon<CartesianLocation> > >&, double)", referenced from:
RadioProximityParticleFilter::RadioProximityParticleFilter(std::__1::list<KnownBluetoothBeacon<CartesianLocation>, std::__1::allocator<KnownBluetoothBeacon<CartesianLocation> > >&, double) in lib.a(RadioProximityParticleFilter.o)
ld: symbol(s) not found for architecture arm64
I know vtable error generally occurs on non-implementing pure virtual functions, but in my case it is same. Any idea where I may be wrong?
Note: The above errors I am getting while integrating my C++ code with objective C in iOS. While in C++ its working fine
This below line is causing the errors in ViewController.mm:
RadioProximityParticleFilter *obj = new RadioProximityParticleFilter (*asList,50);
I completed your code to use it in my MS VS 2013 test project, and it just works - here is the full listing:
#include <iostream>
#include <list>
using namespace std;
class ApproximatePosition
{
public:
static ApproximatePosition *from( int a, int b, int c)
{
cout << "from called." << endl;
return NULL;
}
};
class ListElem{};
template <typename T>
class PositioningMethod
{
public:
virtual ApproximatePosition *getPosition(std::list<T*> &observedRadioSignals) = 0;
};
template <typename T>
class ParticleFilter :public PositioningMethod<T>
{
public:
virtual ApproximatePosition *getPosition(std::list<T*> &observedRadioSignals)
{
/*dummy input and return*/
int a = 0, b = 0, c = 0;
cout << "getPosition called." << endl;
return ApproximatePosition::from( a, b, c );// xxxx, xxxx, xxxx());
}
};
int main()
{
PositioningMethod<ListElem> *pm = new ParticleFilter<ListElem>();
std::list<ListElem*> l;
pm->getPosition( l );
}
The output is:
getPosition called.
from called.

initialize map with static member variable

I do not understand why I cannot use a public const static member of a class in the initializer list of a map (probably any container). As I understand it "MyClass::A" is an rvalue, it seems like it should be the exact same as the case where I am using "THING" which is also a static const just outside of a class.
Here is the error:
Undefined symbols for architecture x86_64:
"MyClass::A", referenced from:
_main in map-380caf.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
And here is the code:
#include <iostream>
#include <map>
#include <string>
static const int THING = 1;
class MyClass {
public:
static const int A = 1;
};
int
main()
{
int a;
typedef std::map<int, std::string> MyMap;
// compiles and works fine
a = MyClass::A;
std::cout << a << std::endl;
// compiles and works fine
MyMap other_map = { {THING, "foo"} };
std::cout << other_map.size() << std::endl;
// Does not compile
MyMap my_map = { {MyClass::A, "foo"} };
std::cout << my_map.size() << std::endl;
return 0;
}
UPDATE 1:
Using clang on OS X:
Apple LLVM version 7.0.0 (clang-700.0.72)
Target: x86_64-apple-darwin14.5.0
Thread model: posix
compiler flags:
clang++ map.cc -std=c++1y
Something in the map code probably tried to take the address of a reference to your int.
The class definition here:
class MyClass {
public:
static const int A = 1;
};
does not actually create any memory for A. In order to do that you have to do in the header file:
class MyClass {
public:
static const int A;
};
and in a CPP file:
const int MyClass::A = 1;
Or I guess with the newest C++ versions you can leave the = 1 in the header and just declare the storage in the CPP file with:
const int MyClass::A;

g++ ld: symbol(s) not found for architecture x86_64 - without more specific error message

I've been trying to solve this problem for hours and hours...
I have a header file, implementation file and a driver file.
HEADER:
class PhoneNumber
{
private:
const int MAXTEXTS;
static int live;
static int text; // number of total texts from all the phones.
string areaCode;
string exchange;
string line;
int nlive;
int ntext; // number of texts sent on this phone
public:
static int MaxPhones;
PhoneNumber();
PhoneNumber(string, string, string, int);
void inputPhoneNumber();
void displayPhoneNumber();
void sendText();
void dialNum();
int getLive();
int getText();
int getnLive();
int getnText();
static void addLive()
{
live++;
}
static void addText()
{
text++;
}
};
IMPLEMENTATION:
int PhoneNumber::getnLive()
{
return nlive;
}
int PhoneNumber::getnText()
{
return ntext;
}
int PhoneNumber::getLive()
{
return live;
}
int PhoneNumber::getText()
{
return text;
}
error message:
habins-mbp:CS2000 Habin$ g++ -o PhoneNumber PhoneNumber.cpp PhoneNumberDriver.cpp
Undefined symbols for architecture x86_64:
"PhoneNumber::live", referenced from:
PhoneNumber::getLive() in PhoneNumber-f64d4d.o
PhoneNumber::addLive() in PhoneNumber-f64d4d.o
"PhoneNumber::text", referenced from:
PhoneNumber::getText() in PhoneNumber-f64d4d.o
PhoneNumber::addText() in PhoneNumber-f64d4d.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
IF I use g++ -c, it compiles meaning that the code works. It seems like the static int live is giving me so much trouble.
Been trying to solve this for 10+ hours and to no avail. I'm about to snap my computer in half!
please help me
You should define all the static member variables in IMPLEMENTATION part.
int PhoneNumber::live;
int PhoneNumber::text;
int PhoneNumber::MaxPhones;

Linking error for a naive singleton class in C++

My code is:
class cMySingleton{
private:
static bool bInstantiated;
int mInt;
cMySingleton(){
mInt=0;
}
public:
cMySingleton(int c){
if (bInstantiated){
cout << "you can only instantiated once";
}
else {
cMySingleton();
mInt=c;
}
}
};
int main () {
cMySingleton s(5);
cMySingleton t(6);
}
The linker keeps complaining:
Undefined symbols for architecture x86_64:
"cMySingleton::bInstantiated", referenced from:
cMySingleton::cMySingleton(int) 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)
What is going on? C++ novice here~~
you should initialize static field.
http://ideone.com/Y1huV
#include <iostream>
class cMySingleton{
private:
static bool bInstantiated;
int mInt;
cMySingleton(){
mInt=0;
}
public:
cMySingleton(int c){
if (bInstantiated){
std::cout << "you can only instantiated once";
}
else {
cMySingleton();
mInt=c;
}
}
};
bool cMySingleton::bInstantiated = true;
int main () {
cMySingleton s(5);
cMySingleton t(6);
}
More information you can be find here:
Static Data Member Initialization
there was also missing include and std:: around cout.
Initialize
static bool bInstantiated;
outside of cMySingleton
bool CMySingleton::bInstantiated;
Dont forget to initialize your static member outside of your class declaration in .cpp file:
bool cMySingleton::bInstantiated = false;

why the following code compiles fine but linking shows error when use static

The following code compiles fine. but when goes to linking,
it shows following error
Undefined symbols for architecture x86_64:
"derived::counter", referenced from:
derived::getAddressCounter() in main.cpp.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
I suspect there is something wrong with the static. but not sure why. Because once I take out the static, the code links fine. But how does static plays any role in this code?
#include <iostream>
#include <string>
struct base_result { };
struct result : public base_result {
int a;
std::string b;
};
struct base {
static base_result counter;
};
struct derived: public base {
static result counter;
result * getAddressCounter(){
counter.a = 10;
counter.b = "haha";
return &counter;
}
};
int main (){
derived d;
result * ptr;
ptr = d.getAddressCounter();
ptr->a = 20;
ptr->b = "baba";
std::cout << ptr->a << std::endl;
std::cout << ptr->b << std::endl;
return 0;
}
struct base
{
static base_result counter;
};
Only declares the static member, You also need to define it once in your cpp file.
Good Read:
What is the difference between a definition and a declaration?
In contrast to member variables which get a reserved space in every created object, static variables can't just be declared, they need to be implemented/defined too.
Just add the lines
base_result base::counter;
result derived::counter;
to your code and it will compile just fine. Those lines instructs the compiler to actually reserve space to store the static variables you declared earlier.