This is the error I am keep getting . undefined reference to my class. I am not sure. I think I m linking them. this is how my main looks like.
#include <iostream>
#include "randomNumberMagnifier.h"
using namespace std;
int main()
{
randomNumberMagnifier r1, r2;
cout << "Random Number "<< r1.getRandomNumber();
cout << endl;
}
I am not sure what I am doing wrong.
this is what it looks like. when I compile
[singha1#cs1 p4]$ g++ -c randomNumberMagnifier.cpp
[singha1#cs1 p4]$ g++ -o p4Driver.cpp
g++: no input files
p4Driver.cpp:(.text+0x8c): undefined reference to `randomNumberMagnifier::getRandomNumber
collect2: ld returned 1 exit status
#ifndef RANDOMNUMBERMAGNIFIER_H
#define RANDOMNUMBERMAGNIFIER_H
class randomNumberMagnifier
{
int addFactor;
int multFactor;
bool addOn;
bool multOn;
int randomNumber;
static const int MAX_ADD_FACTOR = 100;
static const int MAX_MULT_FACTOR = 20;
static const int MAX_RANDOM = 200;
public:
randomNumberMagnifier();
//~randomNumberMagnifer();
randomNumberMagnifier& operator=(const randomNumberMagnifier& rhs);
randomNumberMagnifier(const randomNumberMagnifier& arandom);
randomNumberMagnifier(bool aState, bool mState);
int randomMagnifier();
int getAdd();
int getMult();
bool getAddState();
bool getMultState();
int getRandomNumber();
};
#endif
g++ -o p4Driver.cpp
That doesn't say what it's supposed to compile to, which is what -o is supposed to be for. You want:
g++ -c randomNumberMagnifier.cpp
g++ -c p4Driver.cpp
g++ randomNumberMagnifier.o p4Driver.o -o p4Driver
Or just:
g++ randomNumberMangifier.cpp p4Driver.cpp -o p4Driver
you need to provide randomNumberMagnifier.o to g++ command, so it can find function definition. I tested with below command and i worked
g++ -o p4Driver p4Driver.cpp randomNumberMagnifier.o
Related
I am getting the error shown below while linking the code. How can I fix this problem?
It seems the static variable is not getting initialized.
#include <iostream>
#include <cstdlib>
using namespace std;
struct name{
char c;
};
class List {
static name *a;
public:
static void modify()
{
a = new name();
cout<<"yes";
}
};
name List::*a = NULL;
int main()
{
List::modify();
}
g++ O3 -Wall -c -fmessage-length=0 -o sample.o "..\\sample.cpp"
g++ -o sample.exe sample.o
sample.o:sample.cpp:(.text.startup+0x35): undefined reference to `List::a'
collect2.exe: error: ld returned 1 exit status
name List::*a = NULL; doesn't do what you expected. It defines a global variable named a, which is a pointer to non-static member of List of type name.
The definition of List::a should be
name* List::a = NULL;
Using Clang++ (v3.8.0), the following code fails to link due to sSomeValue being an undefined reference.
#include <iostream>
struct MyClass
{
static constexpr int sSomeSize = 3;
static constexpr int sSomeValue = 10;
};
int foo()
{
int someArray[MyClass::sSomeSize] = {};
std::fill(std::begin(someArray), std::end(someArray), MyClass::sSomeValue);
return someArray[0];
}
int main()
{
std::cout << foo() << std::endl;
}
More precisely:
clang++ -std=c++14 -O2 -Wall -pedantic -pthread main.cpp && ./a.out
/tmp/main-c8de0c.o: In function `foo()':
main.cpp:(.text+0x2): undefined reference to `MyClass::sSomeValue'
/tmp/main-c8de0c.o: In function `main':
main.cpp:(.text+0x16): undefined reference to `MyClass::sSomeValue'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
However, changing the definition of foo to
int foo()
{
int someArray[MyClass::sSomeSize] = {MyClass::sSomeValue, MyClass::sSomeValue, MyClass::sSomeValue};
return someArray[0];
}
does not exhibit the same linker error, even though sSomeValue is still being used.
What is going on here? Is the compiler doing some optimization around the std::fill call that I may not be aware of?
Note that g++ -std=c++14 -O2 -Wall -pedantic -pthread main.cpp && ./a.out compiles, links, and outputs 10 as expected with v6.3.0.
The error is nothing to do with std::fill.
If you refer below documentation, it says: Reference variables can be declared constexpr (their initializers have to be reference constant expressions):
http://en.cppreference.com/w/cpp/language/constexpr
Slight modification of your code works fine. Just make the struct variables "const &" as said above.
#include <iostream>
struct MyClass
{
static constexpr int const& sSomeSize = 3;
static constexpr int const& sSomeValue = 10;
};
int foo()
{
int someArray[MyClass::sSomeSize] = {};
std::fill(std::begin(someArray), std::end(someArray), MyClass::sSomeValue);
return someArray[0];
}
int main()
{
std::cout << foo() << std::endl;
}
Also refer here for well explained article about constexpr and static
"Does static constexpr variable make sense?
Specially the last para in ticked answer.
Have the following program:
#include <iostream>
#include <string>
using namespace std;
class Record
{
public:
static Record* GetInstance(string name);
void printName();
private:
Record(string name);
string name_;
static Record *record;
};
Record::Record(string name)
:name_(name)
{
}
Record*
Record::GetInstance(string name)
{
if(record == NULL) {
record = new Record(name);
}
return record;
}
void
Record::printName()
{
cout << name_ << endl;
}
int main()
{
Record* record1 = Record::GetInstance("sellers");
record1->printName();
Record* record2 = Record::GetInstance("customers");
record2->printName();
}
I am compiling and linking with:
g++ -g -c -Wall main.cpp
g++ -g -Wall main.o -o main
The compilation completes without error(1st command). But the linking is giving this error:
Undefined first referenced
symbol in file
Record::record main.o
ld: fatal: Symbol referencing errors. No output written to main
collect2: ld returned 1 exit status
Wondering how to correct this.
You need to define the variable somewhere, i.e.
Record *Record::record;
Looked around and found a few similar questions but none of them were the same. Most had to do with the constructor or destructor. This issue is, more than likely, a result of my rusty C++ linker memory (picking it back up after a few years).
I'll keep it real simple since this is probably a basic misunderstanding of the linker:
data.h
#pragma once
namespace test {
class Data_V1 {
public:
// some getters/setters
int getData() { return _d; }
void setData( int d ) { _d = d; }
private:
// some data
int _d;
};
}
builder.h
#pragma once
namespace test {
template <class V>
class Builder {
public:
void build();
};
}
builder.cpp
#include <iostream>
#include "builder.h"
namespace test {
template<class V>
void Builder<V>::build() {
std::cout << "Insert building logic" << std::endl;
}
}
main.cpp
#include "builder.h"
#include "data.h"
using namespace test;
int main(int argc, char* argv[]) {
Builder<Data_V1> b;
b.build();
}
compiling:
g++ -Wall -ansi -pedantic -c builder.cpp
g++ -Wall -ansi -pedantic -c main.cpp
g++ -Wall -ansi -pedantic -o main main.o builder.o
Link error:
Undefined symbols for architecture x86_64:
"test::Builder<test::Data_V1>::build()", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
Any help would be appreciated!
Template definitions need to be visible to all translation units. Move the definition from the cpp to the header.
Builder.h
#pragma once
namespace test {
template <class V>
class Builder {
public:
void build();
};
template<class V>
void Builder<V>::build() {
std::cout << "Insert building logic" << std::endl;
}
}
Before you ask, no, there's no way to hide the implementation unless you know all possible specializations beforehand.
Templates represent a generalized form for the creation of a new class. If the implementation is not visible, when you attempt to specialize the template, the compiler can't know what code to generate.
The following are the files involved and a short description:
arrayListType.h, arrayListTypeImp.cpp: declare and implement arrayListType class and its functions.
unorderedarrayListType.h unorderedArrayListTypeImp.cpp: inherit arrayListType class and declare unorderedarrayListType class and implement virtual functions of arrayListType class.
Ch13_Ex6.cpp: Instantiates an object of class unorderedArrayListType and runs some tests.
I am having a compilation error, which I think is due to the Makefile. The following is the error:
unorderedArrayListTypeImp.cpp:4: error: expected unqualified-id before 'using'
unorderedArrayListTypeImp.cpp: In member function 'virtual void unorderedArrayListType::insertAt(int, int)':
unorderedArrayListTypeImp.cpp:11: error: 'cout' was not declared in this scope
unorderedArrayListTypeImp.cpp:11: error: 'endl' was not declared in this scope
unorderedArrayListTypeImp.cpp:13: error: 'cout' was not declared in this scope
Line 4 has a using namespace std; command. The line before that is #include "arrayListType.h". I have tried the following variations in the Makefile but neither worked:
Version 1
all: Ch13_Ex6
arrayListTypeImp.o: arrayListType.h arrayListTypeImp.cpp
g++ -c -Wall arrayListType.h arrayListTypeImp.cpp
unorderedArrayListTypeImp.o: arrayListTypeImp.o unorderedArrayListType.h unorderedArrayListTypeImp.cpp
g++ -c -Wall arrayListTypeImp.o unorderedArrayListType.h unorderedArrayListTypeImp.cpp
Ch13_Ex6.o: Ch13_Ex6.cpp
g++ -c -Wall Ch13_Ex6.cpp
Ch13_Ex6: arrayListTypeImp.o unorderedArrayListTypeImp.o Ch13_Ex6.o
g++ -Wall Ch13_Ex6.o arrayListTypeImp.o unorderedArrayListTypeImp.o -o Ch13_Ex6
Version 2:
all: Ch13_Ex6
arrayListTypeImp.o: arrayListType.h arrayListTypeImp.cpp
g++ -c -Wall arrayListType.h arrayListTypeImp.cpp
unorderedArrayListTypeImp.o: unorderedArrayListType.h unorderedArrayListTypeImp.cpp
g++ -c -Wall unorderedArrayListType.h unorderedArrayListTypeImp.cpp
Ch13_Ex6.o: Ch13_Ex6.cpp
g++ -c -Wall Ch13_Ex6.cpp
Ch13_Ex6: arrayListTypeImp.o unorderedArrayListTypeImp.o Ch13_Ex6.o
g++ -Wall Ch13_Ex6.o arrayListTypeImp.o unorderedArrayListTypeImp.o -o Ch13_Ex6
Both versions compile arrayListTypeImp.o and give the error shown above when compiling unorderedArrayListTypeImp.o. The following is the complete compile output:
make
g++ -c -Wall unorderedArrayListType.h unorderedArrayListTypeImp.cpp
arrayListTypeImp.o
unorderedArrayListType.h:16: error: expected unqualified-id at end of input
unorderedArrayListTypeImp.cpp:4: error: expected unqualified-id before 'using'
unorderedArrayListTypeImp.cpp: In member function 'virtual void unorderedArrayListType::insertAt(int, int)':
unorderedArrayListTypeImp.cpp:11: error: 'cout' was not declared in this scope
unorderedArrayListTypeImp.cpp:11: error: 'endl' was not declared in this scope
unorderedArrayListTypeImp.cpp:13: error: 'cout' was not declared in this scope
unorderedArrayListTypeImp.cpp:13: error: 'endl' was not declared in this scope
Code for arrayListType.h:
#ifndef H_arrayListType
#define H_arrayListType
class arrayListType
{
public:
bool isEmpty() const;
bool isFull() const;
int listSize() const;
int maxListSize() const;
void print() const;
bool isItemAtEqual(int location, int item) const;
virtual void insertAt(int location, int insertItem) = 0;
virtual void insertEnd(int insertItem) = 0;
void removeAt(int location);
int retrieveAt(int location) const;
virtual void replaceAt(int location, int repItem) = 0;
void clearList();
virtual int seqSearch(int searchItem) const = 0;
virtual void remove(int removeItem) = 0;
arrayListType(int size = 100);
arrayListType(const arrayListType& otherList);
virtual ~arrayListType();
protected:
int *list;
int length;
int maxSize;
};
#endif
Code for unorderArrayListTypeImp.cpp:
#include <iostream>
#include "unorderedArrayListType.h"
using namespace std;
void unorderedArrayListType::insertAt(int location,
int insertItem)
{
if (location < 0 || location >= maxSize)
cout << "The position of the item to be inserted "
<< "is out of range." << endl;
else if (length >= maxSize) //list is full
cout << "Cannot insert in a full list" << endl;
else
{
for (int i = length; i > location; i--)
list[i] = list[i - 1]; //move the elements down
list[location] = insertItem; //insert the item at
//the specified position
length++; //increment the length
}
} //end insertAt
void unorderedArrayListType::insertEnd(int insertItem)
{
if (length >= maxSize) //the list is full
cout << "Cannot insert in a full list." << endl;
else
{
list[length] = insertItem; //insert the item at the end
length++; //increment the length
}
} //end insertEnd
// More virtual functions implemented and finally a constructor
unorderedArrayListType::unorderedArrayListType(int size)
: arrayListType(size)
{
} //end constructor
You did not #include <iostream> in arrayListType.h, but did it in arrayListType.cpp, before you #include "arrayListType.h" there. You need to place #include <iostream> into arrayListType.h before you use std::cout or std::endl.
To avoid such mistakes it is good to place the interface header as the first #include statement int the implementation file.
I am guessing that the error is in unorderedArrayListType.h, you likely have a missing semicolon or something. Looking in the Makefile will do nothing to solve that error.
EDIT: Woah, there actually is something wrong with your Makefile! Heh, I just looked at it and you have the following:
g++ -c -Wall arrayListType.h arrayListTypeImp.cpp
Don't pass .h files to g++!, only the .cpp files. So write it like this:
g++ -c -Wall arrayListTypeImp.cpp
Likewise:
g++ -c -Wall unorderedArrayListType.h unorderedArrayListTypeImp.cpp
should be:
g++ -c -Wall unorderedArrayListTypeImp.cpp
Evan Teran is probably right. expected unqualified-id before '...' usually means you missed a semicolon before that line. If it turns out it really is your makefile, here's some general makefile advice:
Use variables. Instead of
g++ -c -Wall ...
you can do
CXX_OPTS= -Wall -O3
...
g++ -c $(CXX_OPTS) ...
Use pattern rules. Rather than
arrayListTypeImp.o: arrayListType.h arrayListTypeImp.cpp
g++ -c -Wall arrayListType.h arrayListTypeImp.cpp
unorderedArrayListTypeImp.o: unorderedArrayListType.h unorderedArrayListTypeImp.cpp
g++ -c -Wall unorderedArrayListType.h unorderedArrayListTypeImp.cpp
Ch13_Ex6.o: Ch13_Ex6.cpp
g++ -c -Wall Ch13_Ex6.cpp
you can use
%.o:%.cpp
g++ $(CXX_OPTS) -c $< -o $#
This should shorten your makefile and make it easier to debug and find your problem. If you want more information, check this out.