Undefined reference to 'HashTable::HashTable()' - c++

I'm working on a testing program for a lab and I keep getting the error
[jereminp#bondi hw3]$ make insert_test
g++ -O3 -g -o insert_test.o -c insert_test.cc
g++ -O3 -g -o insert_test insert_test.o
insert_test.o: In function `main':
/users/ugrad/2018/fall/jereminp/114/hw3/hw3/insert_test.cc:17: undefined reference to `HashTable::HashTable()'
/users/ugrad/2018/fall/jereminp/114/hw3/hw3/insert_test.cc:24: undefined reference to `HashTable::insert(int)'
/users/ugrad/2018/fall/jereminp/114/hw3/hw3/insert_test.cc:32: undefined reference to `HashTable::insert(int)'
collect2: ld returned 1 exit status
make: *** [insert_test] Error 1
I'm fairly certain I have the logic, but I can't get past this error and I feel like I'm missing something simple. Here's the parts of the code in question.
Makefile
CC = g++
CFLAGS =
COPTFLAGS = -O3 -g
insert_test: insert_test.o
$(CC) $(COPTFLAGS) -o $# $^
insert_test.cc
#include "HashTable.h"
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
class HashTable;
int main(int argc, char* argv[])
{
//checks .5 and .9 1x
int numRuns = 0;
int numProbes = 0;
int a=0;
HashTable h;
HashTable.hh
#ifndef HASHTABLE_H
#define HASHTABLE_H
class HashTable
{
public:
HashTable();
/* implement copy constructor, assignment, destructor if needed */
int hashfnc(int key);
int insert (int value);
/* insert the input value and return the number of probes
* return -1 if the table is full and insert fails */
bool find (int value, int& nProbes);
/* Search for the input value in table
* Return true if the search is successful, otherwise false
* Save # probes in 'nProbes' */
// getters
int capacity() { return nSlot; }
int size() { return nElem; }
double load_factor() { return load; }
int getSearchProbes() { return probesSearch; }
private:
/* declare your data */
double load; // track the load factor of table
int nSlot; // # slots i.e. max # elements can hold
int nElem; // current # elements in table
int arr[];
int probesSearch;
};
#endif
HashTable.cc
#include "HashTable.h"
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
class HashTable
{
public:
HashTable()
{
load = 0;
nSlot = 300;//nSlot is changable in order to optimize
nElem = 0;
arr = new int[nSlot];
probesSearch=0;
for(int i = 0;i<nSlot;i++)
{
arr[i] = NULL;
}
}
Some of the things I've tried are adding any and all includes I could think of, changing the last line shown in insert_test.cc to "HashTable h = new HashTable();"(where I got a different error), adding namespace std to the header. I feel like it should be a quick fix but at the same time I can't find the solution anywhere. Plz send help

Related

Restricting __malloc_hook to a particular library

I am trying to use __malloc_hook in our code to manage memory. But It is impacting to client code as well where I don't want to manage memory.
Below is the sample code. :
mathtest.h
int addNumbers(int x, int y);
int subNumbers(int x, int y);
addNumbers.cpp
#include <cstdlib>
#include <iostream>
#include <malloc.h>
using namespace std;
static void my_init_hook (void);
static void *my_malloc_hook (size_t, const void *);
//static void my_free_hook (void*, const void *);
/* Variables to save original hooks */
static void *(*old_malloc_hook)(size_t, const void *);
static void
my_init_hook (void)
{
old_malloc_hook = __malloc_hook;
//old_free_hook = __free_hook;
__malloc_hook = my_malloc_hook;
//__free_hook = my_free_hook;
}
static void *
my_malloc_hook (size_t size, const void *caller)
{
void *result;
/* Restore all old hooks */
__malloc_hook = old_malloc_hook;
// __free_hook = old_free_hook;
/* Call recursively */
result = malloc (size);
/* Save underlying hooks */
old_malloc_hook = __malloc_hook;
//old_free_hook = __free_hook;
/* printf might call malloc, so protect it too. */
printf ("malloc (%u) returns %p\n", (unsigned int) size, result);
/* Restore our own hooks */
__malloc_hook = my_malloc_hook;
//__free_hook = my_free_hook;
return result;
}
int addNumbers(int x, int y)
{
my_init_hook();
int *p = (int*)malloc (100);
cout<<"memory in add numbers : "<<p<<endl;
return x+y;
}
subNumbers.cpp
#include <iostream>
#include <malloc.h>
#include <cstdlib>
using namespace std;
int subNumbers(int x, int y)
{
int *p = (int*)malloc (100);
cout<<"memory in sub numbers : "<<p<<endl;
return x-y;
}
doMath.cpp (Client code)
#include <time.h>
#include <iostream>
#include <cstdlib>
#include "mathtest.h"
using namespace std;
int main()
{
time_t t;
srand((unsigned) time(&t));
int rNuma = rand();
int rNumb = rand();
int answer;
answer = addNumbers(rNuma, rNumb);
cout<<"add = "<<answer<<endl;
answer = subNumbers(rNuma, rNumb);
cout<<"Subtract = "<<answer<<endl;
int *p = (int*)malloc(20000);
cout<<"malloc address in domath : "<<p;
return 0;
}
Header are inside include folder and SO will be kept inside lib folder.
Below are the steps to compile.
g++ -fpic -c addNumbers.cpp subNumbers.cpp
g++ -o libmath.so.1.3.0 addNumbers.o subNumbers.o -fpic -shared -Wl,-soname,libMath.so.1
create soft link
g++ doMath.cpp -o doMath -lMath -I include -L lib
./doMath
Below is the output:
malloc (100) returns 0x564b23f6ee70
memory in add numbers : 0x564b23f6ee70
add = 1549862266
malloc (100) returns 0x564b23f6f2f0
memory in sub numbers : 0x564b23f6f2f0
Subtract = 1535975764
malloc (20000) returns 0x564b23f6f360
__malloc_hook callback is getting called for client code as well. which I don't want.
One possible solution is to reset __malloc_hook callback before calling malloc in client code. But this solution would not work if client code is running on separate thread.
Any possible solution where We can restrict the __malloc_hook so that It doesn't affect the client code. Any help would be much appreciated. Thanks in Advance.

returning a vector in get/set method

I am trying to use a get/set method in c++ for a vector. I keep getting an error throwing an instance of std::out_of_range which occurs when i try to cout the index value of ego_points.at(1) in my main code. I am providing my header file and my main code below but I'll try to explain here:
I have a class called Points with a public function called ScanCallback that builds the vector scannedData and uses the set function to set the private vector transmittedData equal to the scannedData. In my main code, I have an object called p1 of the Points class and I am trying to set a vector called ego_points equal to the transmittedData of object p1. I am able to build the vector ego_points by using the getVector() function but cannot access it's data. Anyone know why not?
Header File:
#pragma once
#include "ros/ros.h"
#include "sensor_msgs/LaserScan.h"
#include "iostream"
#include "string"
#include "motor_driver/Motor_speeds.h"
#include "motor_driver/cartesian.h"
#include <vector>
#ifndef MOTOR_DRIVER_H
#define MOTOR_DRIVER_H
using namespace std;
using namespace motor_driver;
class Points {
public:
vector<float> scannedData;
int i;
int size;
int size1;
int size2;
Points() : scannedData(0) {} //Must match the class name. This is the constructor.
void set(vector<float> transmittingData){
transmittedData = transmittingData;
}
void ScanCallback(const sensor_msgs::LaserScan::ConstPtr& scan) {
scannedData.clear();
size = scan->ranges.size();
scannedData.resize(size);
for(i = 0; i < size; i = i + 1){
scannedData.at(i) = scan->ranges[i];
}
set(scannedData);
}
int getSize(){
return size;
}
vector<float> getVector(){
return transmittedData;
}
private:
vector<float> transmittedData;
};
#endif
Main Code:
#include "ros/ros.h"
#include "sensor_msgs/LaserScan.h"
#include "iostream"
#include "string"
#include "motor_driver/Motor_speeds.h"
#include "motor_driver/cartesian.h"
#include <motor_driver.h>
#include <vector>
using namespace std;
using namespace motor_driver;
Points p1;
int main(int argc, char** argv) {
ros::init(argc, argv, "motor_driver_node");
ros::NodeHandle nh;
ros::Subscriber sub;
ros::Rate r(1);
int ss;
int newsize;
int index;
vector<float> ego_points;
while (ros::ok()) {
sub = nh.subscribe<sensor_msgs::LaserScan>("/scan",10, &Points::ScanCallback, &p1);
newsize = p1.getSize();
ego_points.clear();
ego_points.resize(newsize);
ego_points = p1.getVector();
cout << ego_points.at(1) << endl;
r.sleep();
ros::spinOnce();
}
return 0;
}
My error:
:~/Desktop/Naes_Thesis$ rosrun motor_driver motor_driver_node
terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check
Aborted (core dumped)
does the vector have at least two elements? If you want to access the first element you should use:
cout << ego_points.at(0) << endl;
Also the size method is probably incorrect. It should be something like this:
int getSize() const {
return transmittedData.size();
}
Otherwise the size may contain a different value than the actual vector size
Actually I figured it out. The issue was that the size of the vector is 0 until the vector is built. So I added a if (vector =0) then (do nothing) portion to the code and it fixed it. Thank you Noel-lopes and 1201ProgramAlarm for your contribution.

Debugger unable to show global array values

I am working with Visual Studio 2010 and the code is written in Visual C++. While debugging, if I am declaring my arrays as global I am unable to see their values, while debugging instead it is showing garbage values. I am unable to understand the root cause for this.
If there is any other way to declare the array please do let me know.
This is how I declared the buffers:
#pragma once
#include <windows.h>
#include <msclr/marshal_cppstd.h>
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <iostream>
#include <stdlib.h>
#include<string.h>
char message_buff[1000] = { 0 };
static char buffer[100] = { 0 };
char data[100] = { 0 };//another buffer to store data // int to char changedby me//buffer to store cell ID and LAC
int tower[1000] = { 0 }; // tower no
int signal[1000] = { 0 }; // signal strength
int mccbuff[1000] = { 0 }; // mcc buf
int mncbuff[1000] = { 0 }; // mnc buffer
int count = 0;
int decimal_to_ascii(int value) //logic to get the ascii value from decimal
void clearbuffer(char *p)
{
}
int data_fetch()
{
}
using namespace std;
and that's the output:

Getting a Dwarf Error Version '4'

I am having some trouble with my school project. I think that it may be an error with my use of pointers but I am not sure why I am getting this error. This code is incomplete but I am trying to test it along the way. Do you have any idea why I am getting this error and what does it mean? Thanks!
Error from Cygwin Terminal
-bash-3.2$ make clean
rm -rf *.o simulate
-bash-3.2$ make simulate
g++ -c -g Main.cpp
g++ -c -g Maze.cpp
g++ Main.o Maze.o -o simulate
/usr/bin/ld: Dwarf Error: found dwarf version '4', this reader only handles version 2 information.
Maze.o: In function `Maze::createMaze(char*)':
Maze.cpp:(.text+0x49): undefined reference to `Node::Node(char)'
collect2: error: ld returned 1 exit status
make: *** [simulate] Error 1
Main.cpp
#include <iostream>
#include <fstream>
#include <string>
#include "Maze.h"
using namespace std;
int main()
{
int array_size = 1024;
char * mazeArray = new char[array_size];
int position = 0;
string mazeName;
Maze Maze1;
cout << "Enter the name of the maze file: ";
getline(cin, mazeName);
ifstream fin(mazeName.c_str());
//File opened successfully
if(fin.is_open())
{
while(!fin.eof() && position < array_size)
{
fin.get(mazeArray[position]); //reading one character from file to mazeArray
position++;
}
mazeArray[position-1] = '\0'; //placing character mazeArray terminating character
for(int i = 0; mazeArray[i] != '\0'; i++){
if(isspace(mazeArray[i]))
mazeArray[i] = mazeArray[i+1];
}
cout << "Displaying mazeArray..." << endl << endl;
//this loop display all the charaters in mazeArray till \0
for(int i = 0; mazeArray[i] != '\0'; i++)
{
cout << mazeArray[i];
}
cout << endl;
Maze1.createMaze(mazeArray);
}
else //file could not be opened
{
cout << "File could not be opened." << endl;
}
return 0;
}
Maze.h
#ifndef MAZE_H
#define MAZE_H
#include <string>
#include <sstream>
#include <vector>
#include "Node.h"
using namespace std;
class Maze
{
public:
void createMaze(char*);
void availablePaths();
void displayPath();
void moveNorth();
void moveSouth();
void moveEast();
void moveWest();
int getroomCount();
char getpathHistory();
char getcurrentRoom();
private:
int roomCount;
char pathHistory[];
Node* currentRoom;
Node* nodeArray[12];
struct isPath;
vector<Node*> nodeVector;
};
#endif
Maze.cpp
#include <iostream>
#include <string>
#include "Maze.h"
using namespace std;
Node* Node1;
void Maze::createMaze(char *inFile){
int count = 0;
//Creates Nodes for Maze
for(int ii = 0; ii <= 12; ii++){
Node1 = new Node(inFile[count]);
nodeVector.push_back(Node1);
count = count + 5;
//If there is not another node break out of the for loop.
if(inFile[count] == '\0'){
break;
}
}
}
void Maze::availablePaths(){
}
void Maze::displayPath(){
}
void Maze::moveNorth(){
}
void Maze::moveSouth(){
}
void Maze::moveEast(){
}
void Maze::moveWest(){
}
int Maze::getroomCount(){
}
char Maze::getpathHistory(){
}
char Maze::getcurrentRoom(){
}
Node.h
#ifndef NODE_H
#define NODE_H
#include <string>
#include <map>
#include <sstream>
using namespace std;
class Node
{
public:
Node(char);
void setNodeName(char);
void attachNewNode(Node, int);
Node *getAttachedNode(int);
private:
char name; // Title that is displayed above the menu.
Node *attachedNodes[4];
};
#endif
Node.cpp
#include <iostream>
#include <string>
#include <vector>
#include "Node.h"
using namespace std;
Node::Node(char name) : name(name) {
}
void Node::setNodeName(char tempName){
name = tempName;
}
void Node::attachNewNode(Node temp, int direction){
attachedNodes[direction] = temp;
}
Node Node::getAttachedNode(int direction){
return attachedNodes[direction];
}
makefile
#!/bin/bash
#file:makefile
CC = g++
CFLAGS = -c -g
simulate: Main.o Maze.o
$(CC) Main.o Maze.o -o simulate
Main.o: Main.cpp Maze.h
$(CC) $(CFLAGS) Main.cpp
Maze.o: Maze.cpp Menu.h Node.h
$(CC) $(CFLAGS) Maze.cpp
Node.o: Node.cpp Node.h
$(CC) $(CFLAGS) Node.cpp
clean:
rm -rf *.o simulate
The problem is not in your code, per se.
One problem is that your linking command is failing because you are not linking all your object files. You need to link Main.o, Maze.o and Node.o to create the executable:
g++ Main.o Maze.o Node.o -o simulate
Another problem is that your compiler is newer than your linker. The compiler is generating debugging information using Dwarf version 4, but your linker (/usr/bin/ld) only understands Dwarf version 2.
/usr/bin/ld: Dwarf Error: found dwarf version '4', this reader only handles
version 2 information.
You need to update your linker to a newer version compatible with the compiler you are using.
Or, as janneb suggests in a comment, you can use -gdwarf-2 in both the compilation and link lines. In the makefile:
CFLAGS = -c -gdwarf-2
FILES.cpp = Main.cpp Maze.cpp Node.cpp
FILES.o = $(FILES.cpp:.cpp=.o}
simulate: $(FILES.o)
$(CC) $(CFLAGS) $(FILES.o) -o $#
(There should be few parts of command lines in a makefile that are not macro invocations; then you can change everything more easily.)
You will need to remove the Dwarf-4 object files and recompile to avoid the error/warning.
You can simply fix this by:
export CFLAGS='-gdwarf-2 -gstrict-dwarf'
Then, remake your project.

Compiler error undefined symbol

I have simple class ina header file:
> cat Algorithms.hh
#ifndef Algorithms_hh
#define Algorithms_hh
#include<vector>
class Algorithms
{
public:
Algorithms();
void BubbleSort();
std::vector<int> myarray;
};
#endif
Then a corresponding c file:
> cat Algorithms.cc
#include <iostream>
#include <vector>
#include "Algorithms.hh"
Algorithms::Algorithms()
{
myarray.push_back(0);
}
void Algorithms::BubbleSort()
{
int i, j, flag = 1; // set flag to 1 to start first pass
int temp; // holding variable
int numLength = myarray.size();
for(i = 1; (i <= numLength) && flag; i++)
{
flag = 0;
for (j=0; j < (numLength -1); j++)
{
if (myarray[j+1] > myarray[j]) // ascending order simply changes to <
{
temp = myarray[j]; // swap elements
myarray[j] = myarray[j+1];
myarray[j+1] = temp;
flag = 1; // indicates that a swap occurred.
}
}
}
}
>
And then the main function:
> cat algo2.cc
#include <iostream>
#include <vector>
#include "Algorithms.hh"
using namespace std;
int main(int argc,char **argv)
{
Algorithms *arr=new Algorithms();
arr->myarray.push_back(1);
arr->myarray.push_back(2);
arr->myarray.push_back(100);
return 0;
}
>
When i compile the main:
I get the below error:
> CC algo2.cc
Undefined first referenced
symbol in file
Algorithms::Algorithms() algo2.o
ld: fatal: Symbol referencing errors. No output written to a.out
Can anyone tell me where i am wrong?
This is a linker error, the linker is telling you it can't find the definition of constructor of class Algorithms. You should compile with:
CC Algorithms.cc algo2.cc
You can identify it's a linker error because of the ld: in front of the error.
And of course as stated by Kerrek SB you need to declare your constructor without the Algorithms:: in front of it...
You've just forgotten to include both .cc files into compiling:
cc algo2.cc Algorithms.cc
If you include header file with declarations, like
#include "Algorithms.hh"
you should also provide implementation, definition in .c, or .lib. or load library with definition dynamically. In your case your library is Algorithms.cc, so just add it into compilation stage, and then both temporary object files
Algo2.a + Algorithms.a
will go to
a.out