This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 2 years ago.
I am getting the following error while compiled using gdb:
/tmp/ccYfysv0.o: In function `main':
main.c:(.text+0xf1): undefined reference to `reverse'
collect2: error: ld returned 1 exit status
other answers say it might be due to mispelling of the function name, but here is the code that I am trying to compile.
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int reverse(int,int,int*);
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int temp,n,i;
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
scanf("%d",&a[i]);
int y = reverse(n,(int)0,&a[0]);
int reverse(int o,int k,int* p){
temp = *(p+k);
*(p+k)=*(p+o);
*(p+o) = temp;
o=o-1;k=k+1;
if(o==k)
{
return 0;
}
else if((k+1)==o){
temp = *(p+k);
*(p+k)=*(p+o);
*(p+o) = temp;
return 0;
}
else{
reverse(o,k,p);
}
}
for(i=0;i<n;i++)
printf("%d",a[i]);
return 0;
}
But when I compile it with g++, I get the following error:
expected a ';'
could someone please help me out with this problem.
You can't define reverse inside of main like that. Move it out separate from main, something on this general order:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int reverse(int o,int k,int* p){
temp = *(p+k);
*(p+k)=*(p+o);
*(p+o) = temp;
o=o-1;k=k+1;
if(o==k)
{
return 0;
}
else if((k+1)==o){
temp = *(p+k);
*(p+k)=*(p+o);
*(p+o) = temp;
return 0;
}
else{
reverse(o,k,p);
}
}
int main() {
int temp,n,i;
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
scanf("%d",&a[i]);
int y = reverse(n,(int)0,&a[0]);
for(i=0;i<n;i++)
printf("%d",a[i]);
return 0;
}
You declared a function reverse(), but you did not define it outside of main() (you can't define a function inside of another function, like you did), so the linker can't find the implementation of reverse() to satisfy main's call to it.
Also, your reverse() function is missing a return statement in the final else block.
Try this instead:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int reverse(int,int,int*);
int main()
{
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n,i;
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
scanf("%d",&a[i]);
int y = reverse(n,0,&a[0]);
for(i=0;i<n;i++)
printf("%d",a[i]);
return 0;
}
int reverse(int o,int k,int* p)
{
int temp = *(p+k);
*(p+k)=*(p+o);
*(p+o) = temp;
o=o-1;k=k+1;
if(o==k)
{
return 0;
}
else if((k+1)==o){
temp = *(p+k);
*(p+k)=*(p+o);
*(p+o) = temp;
return 0;
}
else{
return reverse(o,k,p);
}
}
Related
I'm getting this error and I don't know why...
I surfed on google but not got asolution can anyone pls help
I have no clue why is this happening.
I thought that the string return type was messing up but char* return type also didn't help.
#include <iostream>
#include <stack>
#include <string>
using namespace std;
int isoperand(char x){
if(x=='+' || x=='-' ||x=='*' ||x=='/')
return 0;
else
return 1;
}
int precedence(char x){
if(x=='+' || x=='-')
return 1;
else if(x=='*' ||x=='/')
return 2;
return 0;
}
string convert(string infix){
stack<char> s;
string postfix;
int i=0,j=0;
while(infix[i]!='\0'){
if(isoperand(infix[i]))
postfix[j++]=infix[i++];
else{
if(precedence(infix[i])>precedence(s.top()))
s.push(infix[i++]);
else
postfix[j++]=s.pop(); //Error here.
}
}
while(!s.empty()){
postfix[j++]=s.pop();
}
return postfix;
}
int main(){
string infix="a+b*c";
string postfix;
postfix==convert(infix);
cout<<postfix;
return 0;
}
The return type of std::stack::pop() is void. Hence, you may not use:
postfix[j++]=s.pop();
You need to use:
postfix[j++] = s.top();
s.pop();
Since that line is in an else block, you'll have to use:
else
{
postfix[j++] = s.top();
s.pop();
}
Make the same change in the two places in your code that has the same error.
Other errors in your code:
You are accessing postfix using out of bounds indices. Instead of using postfix[j++] = ..., you can use postfix.push_back(...).
You are calling s.top() without checking whether s is empty. When s is empty, the call s.top() throws an exception in my environment.
Here's a fixed up version that works for me.
#include <iostream>
#include <stack>
#include <string>
using namespace std;
int isoperand(char x){
if(x=='+' || x=='-' ||x=='*' ||x=='/')
return 0;
else
return 1;
}
int precedence(char x){
if(x=='+' || x=='-')
return 1;
else if(x=='*' ||x=='/')
return 2;
return 0;
}
string convert(string infix){
stack<char> s;
string postfix;
int i=0;
while(infix[i]!='\0')
{
if(isoperand(infix[i]))
{
postfix.push_back(infix[i++]);
}
else
{
if ( s.empty() )
{
s.push(infix[i++]);
}
else
{
if ( precedence(infix[i])>precedence(s.top()) )
{
s.push(infix[i++]);
}
else
{
postfix.push_back(s.top());
s.pop();
}
}
}
}
while(!s.empty()){
postfix.push_back(s.top());
s.pop();
}
return postfix;
}
int main(){
string infix="a+b*c";
string postfix;
postfix=convert(infix);
cout<<postfix;
return 0;
}
The is the error message:
main.cpp:40:36: error: void value not ignored as it ought to be
postfix[j++]=s.pop(); //Error here.
^
Error message indicates the return-value of a function is 'void', but you are trying to assign it to a non-void variable.
In your case, std::stack<T,Container>::pop() return type is void.
I'm writing a program to reverse the string using a stack. I'm getting 2 errors in my code. 1. no operator >> matches the operand 2. On the line Reverse(string); it errors say (string)type name is not allowed. Any idea why?
#include <iostream>
#include <string.h>
#include <stack>
using namespace std;
void Reverse(char);
int main()
{
char object;
cout << "Please enter a line of text.\n";
cin >> object;
char * point = new char[object.length() + 1];
strcpy(point, object.c_str());
Reverse(point);
Reverse(point);
printf(" %s", object);
system("pause");
return 0;
}
void Reverse(char *p)
{
stack<char> S;
for (int i = 0; i<strlen(p); i++)
S.push(p[i]);
for (int i = 0; i<strlen(p); i++)
{
p[i] = S.top();
S.pop();
}
}
updated code: error on cin >> object says the initial question no operator matches an operand
#include <iostream>
#include <string.h>
#include <stack>
using namespace std;
void Reverse(string);
int main()
{
string object;
cout << "Please enter a line of text.\n";
cin >> object;
char * point = new char[object.length() + 1];
strcpy(point, object.c_str());
Reverse(point);
printf(" %s", point);
system("pause");
return 0;
}
void Reverse(char *p)
{
stack<char> S;
for (int i = 0; i<strlen(p); i++)
S.push(p[i]);
for (int i = 0; i<strlen(p); i++)
{
p[i] = S.top();
S.pop();
}
}
I am getting error strcpy_s does not take 2 arguments.
#include <iostream>
#include <cstring>
#include <string>
#include <stack>
using namespace std;
void Reverse(char *p)
{
stack<char> S;
for (int i = 0; i<strlen(p); i++)
S.push(p[i]);
for (int i = 0; i<strlen(p); i++)
{
p[i] = S.top();
S.pop();
}
}
int main()
{
string object;
cout << "Please enter a line of text.\n";
cin >> object;
char * point = new char[object.length() + 1];
strcpy_s(point, object.c_str());
Reverse(point);
printf(" %s", point);
system("pause");
return 0;
}
char * point = new char[object.length()+1];//+1 for the null terminator
strcpy(point, object.c_str());
Reverse(point);
printf("%s",point);
allocates space for point then copies http://en.cppreference.com/w/cpp/string/byte/strcpy and you were calling Reverse like this Reverse(char) you need to call it using the name of the char variable like this Reverse(point); since we allocated space we need to delete it after we are done using it.
Please check the modified code below. I can reverse the input string now.
#include <iostream>
#include <string>
#include <stack>
using namespace std;
void Reverse(char *p)
{
stack<char> S;
for (int i = 0; i<strlen(p); i++)
S.push(p[i]);
for (int i = 0; i<strlen(p); i++)
{
p[i] = S.top();
S.pop();
}
}
int main()
{
string object;
cout << "Please enter a line of text.\n";
cin >> object;
char * point = new char[object.length() + 1];
strcpy(point, object.c_str());
Reverse(point);
printf(" %s", point);
system("pause");
return 0;
}
I get this error when i build the program: Apple Mach-O Linker (ld) Error Linker Command failed with exit code 1. Usually when I try to fix this error, its because the file with the main function is #include-ing a file more than once. However, I do not believe that that is the case this time. I am also notified by X Code that the duplicate symbol _passed in: ranker.o and olympic.o.
//competitor.h
#ifndef __Olympic_Lab__competitor__
#define __Olympic_Lab__competitor__
#include <iostream>
using namespace std;
class Competitor {
char* name = nullptr;
int laneAssignment;
float time;
public:
Competitor(char n[], int lane);
~Competitor();
void setTime(float f);
char* getName(){ return name; }
int getLane(){ return laneAssignment; }
float getTime(){ return time; }
void print(){ cout << name << endl; }
};
#endif
//competitor.cpp
#include "competitor.h"
Competitor::Competitor(char n[], int lane){
name = n;
laneAssignment = lane;
}
Competitor::~Competitor(){
//does nothing for now
}
void Competitor::setTime(float t){
time = t;
}
//ranker.h
#ifndef __Olym__ranker__
#define __Olym__ranker__
#include <vector>
#include "competitor.h"
using namespace std;
int passed = 0;
class Ranker {
bool boolean = true;
public:
vector<Competitor*> rv;
Ranker(int lanes);
~Ranker();
int addList(Competitor* c);
Competitor* getLane(int lane);
Competitor* getFinish(int finish);
int getFilled();
};
#endif
//ranker.cpp
#include "ranker.h"
Ranker::Ranker(int lan){
rv.resize(lan - 1);
for(int i = 0; i <= rv.size(); i++){
rv[i] = nullptr;
}
}
Ranker::~Ranker(){
for(int i = 0; i <= rv.size(); i++){
delete rv[i];
}
}
int Ranker::addList(Competitor *c){
if(c != NULL && passed <= 4){
rv[passed++] = c;
return passed - 1;
}
return 0;
}
Competitor* Ranker::getLane(int lane){
for(int i = 0; i <= rv.size(); i++){
if(rv[i]->getLane() == lane && rv[i] != NULL){
return rv[i];
}
}
return rv[0];
}
Competitor* Ranker::getFinish(int finish){
if(boolean){
Competitor *temp = nullptr;
int highestIndex;
for(int i = rv.size(); i >= 0; i--){
highestIndex = i;
for(int j = i; j >= 0; j--){
if(rv[j] != nullptr && rv[highestIndex] != nullptr){
if(rv[j]->getTime() > rv[highestIndex]->getTime())
highestIndex = j;
}
}
temp = rv[i];
rv[i] = rv[highestIndex];
rv[highestIndex] = temp;
}
delete temp;
temp = *new Competitor*;
boolean = false;
}
return rv[finish - 1];
}
int Ranker::getFilled(){
int filled = 0;
for(int i = 0; i <= rv.size(); i++){
if(rv[i] != NULL){
filled++;
}
}
return filled;
}
//olympic.h
#ifndef _Olympic_Lab__olympic__
#define _Olympic_Lab__olympic__
#include "ranker.h"
#endif
//olympic.cpp
#include "olympic.h"
int main(){
const int lanes = 4;
Ranker rank(lanes);
Competitor* starters[4];
starters[0] = new Competitor("EmmyLou Harris", 1);
starters[1] = new Competitor("Nanci Griffith", 2);
starters[2] = new Competitor("Bonnie Raitt", 3);
starters[3] = new Competitor("Joni Mitchell", 4);
starters[0]->setTime((float)12.0);
starters[1]->setTime((float)12.8);
starters[2]->setTime((float)11.0);
starters[3]->setTime((float)10.3);
for(int i = 0; i < lanes; i++){
rank.addList(starters[i]);
}
cout << "Competitors by lane are:" << endl;
for(int i = 1; i <= lanes; i++)
rank.getLane(i)->print();
cout << "Rankings by finish are:" << endl;
for(int i = 1; i <= lanes; i++)
rank.getFinish(i)->print();
for(int i = 0; i < lanes; i++){
delete starters[i];
}
}
It would be appreciated if someone could assist me in finding exactly what causes this error. Thanks!
If you declare a variable in a header it will be duplicated in every file that includes the header.
So int passed = 0; in ranker.h is going to give you a lot of grief. ranker and olympian both have allocated a different passed and the linker now has no clue which one is the real passed.
So what you probably want is
extern int passed;
in ranker.h to declare that passed will exist at some point, if it doesn't yet, so it doesn't need to be allocated. The compiler will happily carry on and allow you to use passed.
And then in ranker.cpp, declare
int passed = 0;
to allocate passed and satisfy the linker. Now you have only one passed and anyone who includes ranker.h can see and use it.
If you want more than one passed, you have to do something else so that they don't share the same name and scope, but this doesn't look like your goal.
Off topic: resist the urge to put using namespace std;in a header file. It can lead to future problems that can be very hard to debug. More here: Why is "using namespace std" considered bad practice?
So when I try to simply compile my code using "g++ Asg5.cpp" I receive the following error
/tmp/cczhpSGO.o: In function 'main':
Asg5.cpp:(.text+0x2fb): undefined reference to 'BinomialTree::insert(int)'
collect2: ld returned 1 exit status
If anyone's wondering why I'm not using a makefile, my professor simply wants to type g++ <.cpp with main()> to compile..
Anyway here's my code I really appreciate the assistance!
Asg5.cpp
#include "BinomialTree.h"
#include "BinomialNode.h"
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <stdlib.h>
#include <string>
#include <stdio.h>
using namespace std;
int main(int argc, char* argv[])
{
//input handling
if(argc != 2)
{
cout << "Incorrect Usage. \n Example: ./a.out <filename>" << endl;
exit(1);
}
BinomialTree *tree = new BinomialTree();
char *buffer;
char *token;
//read file into buffer.**************************************
string input;
ifstream file;
file.open(argv[1]);
if(file.is_open())
{
string str;
while(file.good())
{
getline(file,str);
input += " " + str;
}
}
else{
cout << "File not found"<< endl;
return 1;
}
file.close();
int buf;
stringstream ss(input);
vector<int> tokens;
while(ss >> buf)
{
tokens.push_back(buf);
}
int i = 0;
for(i = 0; i < tokens.size(); i++)
tree->insert(tokens[i]);
//end file reading *******************************************
delete tree;
}
BinomialNode.h
#ifndef _BINOMIALNODE_H_
#define _BINOMIALNODE_H_
#include "BinomialTree.h"
class BinomialNode
{
public:
int k;
BinomialNode *children[20];
int data;
BinomialNode();
};
#endif
BinomialNode.cpp
class BinomialNode
{
BinomialNode::BinomialNode(int n)
{
this->k = 0;
this->data = n;
}
}
BinomialTree.h
#ifndef _MULTIMAP_H_
#define _MULTIMAP_H_
#include "BinomialNode.h"
class BinomialTree
{
public:
BinomialNode * BQ[20];
void insert(int n);
void merge(BinomialNode *queue, BinomialNode *in, int k);
void print(BinomialNode *root, int tab);
};
#endif
BinomialTree.cpp
#include "BinomialNode.h"
#include "BinomialTree.h"
#include <iostream>
#include <cstdlib>
class BinomialTree
{
void BinomialTree::insert(int n)
{
BinomialNode *in = new BinomialNode(n);
if(BQ[0] == NULL)
{
BQ[0] = in;
return;
}
else
merge(BQ[0], in, 0);
}
void BinomialTree::merge(BinomialNode *queue, BinomialNode *in, int k)
{
if(queue == NULL)
{
BQ[k] = in;
return;
}
if(n == NULL)
{
BQ[k] = queue;
return;
}
if(queue->data > in->data)
{
merge(in, queue);
return;
}
queue->k++;
BinomialNode* temp[queue->k];
int i;
for(i = 0; i < queue->k-1; i++)
temp[i] = queue->children[i];
temp[queue->k-1] = in;
for(i = 0; i < queue->k; i++)
queue->children[i] = temp[i];
if(BQ[queue->k] == NULL)
{
BQ[queue->k] = queue;
return;
}
else
merge(queue, BQ[queue->k]);
}
void BinomialTree::print(BinomialNode *root, int tab)
{
if(root == NULL)
return;
int i;
for(i = 0; i < tab*5; i++) cout << " ";
cout << root->data << endl;
for(i = 0; i < root->k; i++) print(root->children[i], tab+1);
}
}
You cpp files shouldn't have Class in them. They should look more like:
BinomialNode.cpp
#include "BinomialNode.h"
BinomialNode::BinomialNode(int n) :
k(0)
{
data = n;
}
And of course the corollary for the much longer BinomialTree.cpp. Also, you should compile it with something like:
g++ BinomialTree.cpp BinomialNode.cpp Asg5.cpp -o asg5
Also you're going to run into a lot of other problems with you code. For instance:
BinomialNode * BQ[20];
I don't see BQ being initialized anywhere, which means you're pretty much guaranteed a seg fault if you were to run this. You need to initialize this or allocate it. Seeing lines like:
if(BQ[0] == NULL)
Makes me think you really wanted:
BinomialNode BQ[20];
Though you would still need to initialize it to all NULLs since you aren't guaranteed that will be full of NULLs when you run the program. Also, this is recursive and infinite and can't possibly work (in BinomialNode.h):
BinomialNode *children[20];
There are likely more issues with this code, but that wasn't your question, so I'll stop now!
You need to compile all the .cpp files into one program. You're not using BinomialTree.cpp anywhere right now. If your project requires that you simply type g++ Asg5.cpp then you need to #include your .cpp files or move their contents to your .h files. But the normal solution would be to build all the .cpp files together, not just the one containing main().
So I was trying to count the total number of negative numbers in the stack and I wrote this code but something went wrong with it and its showing no output. I am a beginner in c++ so I am sure it will be quite wrong.
#include <iostream>
#include <stack>
using namespace std;
size_t i(stack<int> s){
int count=0;
while(s.size() !=0){
if(s.top()<0){
count++;
s.pop();
} else if(s.top()>0){
s.pop();
} else{}
cout<<count<<endl;
}
return count;
}
int main(){
stack<int> s;
s.push(-1);
s.push(2);
s.push(-2);
size_t i(stack<int> s);
return 0;
}
In your main() function you do not call i() you simply redeclare it.
#include <iostream>
#include <stack>
using namespace std;
size_t i(stack<int> s){
int count=0;
while(s.size() !=0){
if(s.top()<0){
count++;
s.pop();
} else if(s.top()>0){
s.pop();
} else{}
cout<<count<<endl;
}
return count;
}
int main(){
stack<int> s;
s.push(-1);
s.push(2);
s.push(-2);
size_t i(stack<int> s); // this DOES NOT call the function
i(s); // <== THIS calls the function!!!
return 0;
}
Your statement size_t i(stack<int> s); does not call the function, it simply tells the compiler what parameters it accepts and what its return type is.