Collect2: error reference method without defining - c++14

I'm building a basic program to make a list that consists of objects in c++, I've looked over the code several times and am just stuck, my professor isn't answering and have to turn this in soon, if anyone can help me find why it's saying that getTotal2020() is not defined when clearly it is. I am compiling with all the files and the #include is in all the files as well.
I've tried different ways of accessing this method but every time it throws errors. At least how I have the code now, I don't have any errors.
class Data {
public:
Data(string neighborhood, int total2020, int numOfMen, int numOfWomen);
string neighborhood;
int total2020;
int numOfMen;
int numOfWomen;
~Data();
string getNeighborhood();
int getTotal2020();
int getNumOfMen();
int getNumOfWomen();
};
Data::Data(string neighborhood, int total2020, int numOfMen, int numOfWomen) {
this->neighborhood;
this->total2020;
this->numOfMen;
this->numOfWomen;
}
string getNeighborhood(string neighborhood){
return neighborhood;
}
int getTotal2020(int total2020){
return total2020;
}
int getNumMen(int numOfMen){
return numOfMen;
}
int getNumOfWomen(int numOfWomen){
return numOfWomen;
}
class Node{
public:
Node* next;
Data* data;
Node(Node* next, Data* data){
this->next = next;
this->data = data;
}
Node* getNode(){
return next;
}
Data* getData(){
return data;
}
};
void List::findElement(int findMinPop){
Node* aux;
aux = head;
while(aux){
if(aux->getData()->getTotal2020() >= findMinPop){
cout << "The population is greater" << endl;
} else {
cout << "The populations is less than you were looking for" << endl;
}
aux = aux->next;
}
}
The error is gives me is:
/tmp/ccJeheba.o:main.cpp:(.text+0x3ed): referencia a `Data::getTotal2020()' sin definir (that the method is not defined)
/tmp/ccJeheba.o:main.cpp:(.text+0x3ed): reubicación truncada para ajustar: R_X86_64_PC32 contra el símbolo `Data::getTotal2020()' sin definir
collect2: error: ld devolvió el estado de salida 1

Related

runtime error: addition of unsigned offset to 0x602000000390 overflowed to 0x60200000038c

I was solving a question named as. Find First and Last Position of Element in Sorted Array
'''
void bin_search(vector<int>&nums,int target,int l,int h,int &a,int &b)
{
if(l>h)
return;
int mid= l + (h - l) / 2;
if(nums[mid]==target)
{
if(nums[mid-1]==target)
{
bin_search(nums,target,l,mid-1,a,b);
}
else
{
a=mid;
}
if(nums[mid+1]==target)
{
bin_search(nums,target,mid+1,h,a,b);
}
else
{
b=mid;
}
}
else if(nums[mid]<target)
{
bin_search(nums,target,mid+1,h,a,b);
}
else
{
bin_search(nums,target,l,mid-1,a,b);
}
}
class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
int a=-1;
int b=-1;
int l=0;
int h=nums.size()-1;
bin_search(nums,target,l,h,a,b);
return {a,b};
}
};
'''
this is the code which i submitted but i am getting
Line 1034: Char 34: runtime error: addition of unsigned offset to 0x602000000390 overflowed to 0x60200000038c (stl_vector.h)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/stl_vector.h:1043:34
error i am able to pass all the test cases in my complier and other id but didn't able to solve this bug
please help me thanks in advance

Why Numbers are getting printed in the form like "12-256" , "13-256" rather than 12,13 etc.?

I'm solving one of the algorithms problem from university to implemet queue using stacks.
I've got my logic right i guess but the numbers are getting printed in the form of 12-256, 13-256, 14-256 instead of 12,13,14.
Here's my C++ Code,
#include <iostream>
using namespace std;
class Stack{
private:
int arr[200];
int tos = -1;
public:
bool empty(){
return (tos == -1)?true:false;
}
void push(int element){
arr[++tos] = element;
}
int pop(){
return arr[tos--];
}
void show(){
if(tos == -1){
cout<<"stack empty";
}else{
for(int i=tos;i>0;i--)
cout<<arr[i]<<"\t";
}
}
};
class Queue{
private:
Stack s1,s2;
public:
void enQueue(int x){
//just using s1 to add new elements
s1.push(x);
}
int deQueue(){
if(s1.empty())
throw 'e';
else{
int e;
while(!s1.empty()){
e = s1.pop();
s2.push(e);
}
cout<<"\nelement to be removed:"<<s2.pop();
if(s2.empty())
throw 'f';
else{
int e;
while(!s2.empty()){
e = s2.pop();
s1.push(e);
}
}
}
}
};
int main()
{
try{
Queue q1;
q1.enQueue(12);
q1.enQueue(13);
q1.enQueue(14);
q1.enQueue(15);
cout<<q1.deQueue();
cout<<q1.deQueue();
cout<<q1.deQueue();
cout<<q1.deQueue();
}catch(char c){
cout<<"\nstack empty!";
}
return 0;
}
I'm basically a Python Guy so i'm not able to figure out what's wrong with this code.
I'm new to C++, so please guide me through this.
Thanks in advance!
deQueue suffers from the following problems.
It doesn't return anything.
It's OK for s2 to be empty after its top has been popped.
Here's an updated version that should work.
int deQueue(){
if(s1.empty())
throw 'e';
int e;
while(!s1.empty()){
e = s1.pop();
s2.push(e);
}
int ret = s2.pop();
cout<<"\nelement dequeued:"<< ret;
// This is not correct.
// It's OK for s2 to be empty after its top has been popped.
// if(s2.empty())
// throw 'f';
while(!s2.empty()){
e = s2.pop();
s1.push(e);
}
return ret;
}
Suggestion for further improvement
Queue does not need two Stack objects as member variables. s2 can be a function local variable in deQueue.
class Queue
{
private:
Stack s;
...
};
If you decide to make that change, you'll have to update enQueue and deQueue accordingly.

Error in code when searching through the right subtree in my binary search tree

In one of my classes at Uni we are creating Binary search trees and inserting data and looking them up. My code make sense in my head and because of this I cannot find the error anywhere. I have spent ages trying to find the error but cannot find it anywhere. The only thing that might be causing an error is that the precompiled headers didn't work when I started so i removed them from my project. The error only occurrs when i try to use the BST.Lookup and choose a key that is on the right subtree.
This is my main cpp file:
// BinarySearchTrees.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include "BST.h"
#include <iostream>
#include <fstream>
#include <string>
void ReadFile(BST &Bst)
{
int iKey;
std::string Key;
std::string Data;
std::ifstream testFile("Test.txt");
if (testFile.is_open())
{
while (!testFile.eof())
{
getline(testFile, Key, ' ');
getline(testFile, Data);
iKey = stoi(Key);
Bst.Insert(iKey, Data);
}
}
}
int main()
{
std::string Option;
int Choice;
BST BST;
//ReadFile(BST);
BST.Insert(6, "Oscar");
BST.Insert(20, "Ben");
BST.Insert(99, "James");
BST.Insert(1, "Alex");
while (Option != "exit")
{
std::cout << "If you wish to Lookup a Node, Insert value to find. Enter 'exit' to close" << std::endl;
getline(std::cin, Option);
if (Option == "exit")
break;
else
{
Choice = stoi(Option);
BST.Lookup(Choice);
}
}
return 0;
}
I believe that the readfile code may be incorrect but am unsure.
My Binary Search Tree Class:
#include "BST.h"
struct BST::Node {
Key key;
Item item;
Node* leftChild;
Node* rightChild;
Node(Key, Item);
};
void BST::Insert(Key inputKey, Item inputItem)
{
Node* previousNode = nullptr;
if (root == nullptr)
{
root = new Node(inputKey, inputItem);
}
else
{
InsertRec(inputKey, inputItem, root, previousNode);
}
}
void BST::InsertRec(Key inputKey, Item inputItem, Node* & Current, Node* & previousNode)
{
if (Current != nullptr)
{
previousNode = Current;
}
bool isLeft = false;
if (!isLeaf(Current))
{
if (inputKey > Current->key)
{
isLeft = false;
InsertRec(inputKey, inputItem, Current->rightChild, previousNode);
}
else if (inputKey < Current->key)
{
isLeft = true;
InsertRec(inputKey, inputItem, Current->leftChild, previousNode);
}
else
{
Current->item = inputItem;
}
}
else
{
Current = new Node(inputKey, inputItem);
if (isLeft)
{
previousNode->leftChild = Current;
}
else
{
previousNode->rightChild = Current;
}
}
}
BST::Item* BST::Lookup(Key soughtKey)
{
Item* Item = LookupRec(soughtKey, root);
std::string Display = /*std::to_string(soughtKey) + ": " + */ *Item;
std::cout << Display << std::endl;
return Item;
}
BST::Item* BST::LookupRec(Key soughtKey, Node* currentNode)
{
if (!isLeaf(currentNode))
{
if ((currentNode->key > soughtKey))
{
LookupRec(soughtKey, currentNode->leftChild);
}
else if ((currentNode->key < soughtKey))
{
LookupRec(soughtKey, currentNode->rightChild);
}
else
{
return &currentNode->item;
}
}
else
{
return nullptr;
}
}
bool BST::isLeaf(Node* n)
{
if (nullptr == n)
{
return true;
}
else
{
return false;
}
}
BST::BST()
{
}
BST::Node::Node(Key K, Item I)
{
key = K;
item = I;
leftChild = nullptr;
rightChild = nullptr;
}
And finally my header file for the binary search tree:
#pragma once
#include "iostream"
#include "string"
class BST
{
public:
using Key = int;
using Item = std::string;
void Insert(Key, Item);
Item* Lookup(Key);
BST();
private:
struct Node;
Node* root = nullptr;
static bool isLeaf(Node*);
static Item* LookupRec(Key, Node*);
static void InsertRec(Key, Item, Node* &, Node* &);
};
Any help would be greatly appreciated. I've been stuck on this for too long and I cannot progress without fixing this first.
The Test.txt file is filled with keys and items that are read and inputted like how I do it manually at the start of my main function, so I dont think the error is the file data.
Thanks in advance
UPDATE: Have finally found the error. The problem was with the bool isLeft in my InsertRec function. The bool was always false due to the recursion so have changed the code to compare previousNode->Key with Current->Key to determine if the child goes left or right

Why is this code working in a different way than expected? I think its working right, is it?

Here is what I am supposed to do.
Here is what I did.(on codeblocks IDE with minGW compiler)
#include <iostream>
using namespace std;
class person
{
private:
string name;
int age;
double height;
double weight;
public:
void set_private_members(string n, int a, double h, double w)
{
name = n;
age = a;
weight = h;
height = w;
}
void print_private_members()
{
cout <<"The name of person is:- "<<name<<"\n The age of person is:- " <<age<<"\n The weight of person is:- "<<weight;
cout <<"\n The height of person is:- "<<height<<endl;
}
};
void modify_person(person);
int main()
{
person p;
p.set_private_members("Ayush",19,165.7,47.2);
cout <<"We are in main function right now \n";
p.print_private_members();
modify_person(p);
cout <<"We are now back in main function \n The values of object passed to modify_person function are as \n";
p.print_private_members();
return 0;
}
void modify_person(person z)
{
cout <<"We are in modify_person function \n Modifying person details... \n";
z.set_private_members("Priyanshi",15,159.1,50.6);
cout <<"The details are now as:- \n";
z.print_private_members();
}
Here is the output to this code
however the expected output is to have priyanshi details in the class object when print_private_members function is called in main() for the last time.
Who is wrong? Me or they? I think if I call modify_person by reference then the expected should come.

Function move could not be resolved, Semantic Error

The following program which is supposed to emulate std::vector. I am using Eclipse IDE for C/C++ Developers
Version: Neon.3 Release (4.6.3)
Build id: 20170314-1500
and my c++ version is g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
is flagging error that "function std::move could not be resolved".
What is the possible reason for this error ?
//============================================================================
// Name : data_structure_1.cpp
// Author : Manish Sharma
// Description : Programme to implement a simple vector class named "Vector".
// Reference : Data Structures and Algo. analysis in c++, Mark Allen Weiss
//============================================================================
#include <iostream>
#include <algorithm>
using namespace std;
template<class Object>
class Vector{
public:
// constructor
explicit Vector(int initSize = 0):
theSize{initSize},
theCapacity{initSize + SPARE_CAPACITY},
objects{new Object[theCapacity]}{
}
// copy constructor
Vector(const Vector& rhs):
theSize{rhs.theSize},
theCapacity{rhs.theCapacity},
objects{new Object[theCapacity]}{
for(int k = 0;k<theSize; ++k){
objects[k] = rhs.objects[k];
}
}
// copy assignment operaor
Vector & operator= (const Vector & rhs){
Vector copy = rhs;
std::swap(*this,copy);
return *this;
}
//class destructor
~Vector(){
delete[] objects;
}
//c++ 11 additions, reference to rvalues
Vector(Vector&& rhs) :
theSize{rhs.theSize},
theCapacity{rhs.theCapacity},
objects{rhs.objects}{
cout<<endl<<"Inside lvalue reference constructor";
//if you forget to include this then when rhs will you destroyed
//you will be left with a dangling pointer
rhs.objects = nullptr;
rhs.theSize = 0;
rhs.theCapacity = 0;
}
// copy assignment operaor
Vector & operator= (Vector && rhs){
cout<<endl<<"Inside lvalue reference copy";
Vector copy = rhs;
std::swap(*this,copy);
return *this;
}
void resize(int newSize){
if(newSize > theCapacity)
reserve(newSize*2);
theSize = newSize;
}
void reserve(int newCapacity){
if(newCapacity<theSize)
return;
Object *newArray = new Object[newCapacity];
cout<<endl<<"moving inside reserve";
for(int k=0;k<theSize;++k){
newArray[k] = std::move(objects[k]);
}
theCapacity = newCapacity;
std::swap(objects,newArray);
delete[] newArray;
}
//Some extra useful functions
int size() const{
return theSize;
}
bool empty() const{
return size()==0;
}
int capacity() const{
return theCapacity;
}
void increaseCapacity(){
reserve(2*theCapacity+1);
}
//insertion and deletion functions
void push_back(const Object & x){
if(theSize == theCapacity){
increaseCapacity();
}
cout<<endl<<"Moving inside push_back";
objects[theSize++] = std::move(x);
}
void pop_back(){
--theSize;
}
using iterator = Object*;
using const_iterator = const Object*;
iterator begin(){
return &objects[0];
}
const_iterator begin() const{
return &objects[0];
}
iterator end(){
return &objects[size()];
}
const_iterator end() const{
return &objects[size()];
}
//class specific constants
static const int SPARE_CAPACITY = 16;
private:
int theSize;
int theCapacity;
Object * objects;
};
int main() {
Vector<int> my_vector;
my_vector.push_back(10);
int j{24};
my_vector.push_back(j);
for(int i = 0;i<20;i++){
my_vector.push_back(i*10);
}
cout<<"\nSize = "<<my_vector.size()<<endl;
my_vector.capacity();
for(auto it = my_vector.begin();it!=my_vector.end();++it){
cout<<*it<<", ";
}
return 0;
}

Resources