0

I am new to coding in C++, and trying to teach myself for class. I need to create a menu driven program that will execute two functions, one to see if a string is a palindrome and the other to see find the greatest common denominator of two numbers.

I have a main.cpp, a GCD.cpp, a palindrome.cpp, a GCD.h and a palindrome.h. When I compile on the command line I get the following error:

/tmp/ccVf007n.o: In Function 'main': main.cpp: (.test+0x75): undefined reference to euclid(int, int); collect2: error: ld returned 1 exit status.

My code blocks are: main.cpp

#include <iostream>
#include "palindrome.h"
#include "GCD.h"
#include <string>
using namespace std;

void showChoices();
int x,y;

int main() {

int choice;
do
{
showChoices();
    cin >> choice;
    switch (choice)
    {
        case 1:
            cout << "Palindrome Program.";
            main();
            break;

        case 2:
            cout << "Greatest Common Denominator Program.";
            euclid(x,y);
            break;

        case 3:
            break;
    }

}while (choice !=3 );
}

void showChoices(){

cout << "Menu" << endl;
cout << "1. Palindrome Program" << endl;
cout << "2. Greatest Common Denominator Program" << endl;
cout << "3. Exit" << endl;
}

GCD.cpp

#include <iostream>
using namespace std;

int euclid (int*, int*);
int main() {
int a, b;
cout << "A program to find GCD of two given numbers.";
cout << "\n\nEnter your choice of a number: ";
cin >> a;
cout << "\nEnter your choice of another number: ";
cin >> b;

cout << "\n\nProcessing with Euclid method";
cout << "\nThe GCD of " << a << " and " << b << " is " << euclid(a, b);
return 0;
}

int euclid ( int *x, int *y) {
if ( x % y == 0 )
    reutrn y;
else return euclid ( y, x%y );

}

palindrome.cpp

#include<iostream>
using namespace std;

int main(){
char string1[20];
int i, length;
int flag = 0;

cout << "Enter a string: ";
cin >> string1;

length = strlen(string1);

for(i=0;i < length ;i++){
    if(string1[i] != string1[length-i-1]){
        flag = 1;
        break;
    }
}

if (flag) {
    cout << string1 << " is not a palindrome" << endl;
}
else {
    cout << string1 << " is a palindrome" << endl;
}
system("pause");
return 0;
}

GCD.h

#ifndef PROJ4_GCD_H
#define PROJ4_GCD_H

int euclid (int, int);

#endif //PROJ4_GCD_H

palindrome.h

#ifndef PROJ4_PALINDROME_H
#define PROJ4_PALINDROME_H

char string1[20];
int i, length;
int flag = 0;

#endif //PROJ4_PALINDROME_H

I appreciate all input and help. Thanks,

  • As you have 3 cpp files, can you add to your post what linking command you use? Actually, add all the commands you use for compiling/linking everything. – Adrian Colomitchi Sep 25 '16 at 23:42
  • Mate, there can be a single `main` function in an executable. You have 3, one for each file. Decide which one should be the actual main and which are the ones you need to rename. – Adrian Colomitchi Sep 25 '16 at 23:49
  • I use g++ -o main main.cpp to compile it. And I changed the others to match the names of the .cpp files. – T. Murray Sep 25 '16 at 23:53
  • Well, that will create troubles, because the euclid function is implemented in `GCD.cpp` and yet you tell the compiler "Generate for me an executable from main.cpp only" Maybe [this](http://stackoverflow.com/questions/6264249/how-does-the-compilation-linking-process-work) will help after reading it. – Adrian Colomitchi Sep 25 '16 at 23:58
  • And when you get to: 'g++ -o main main.cpp GDC.cpp palindrome.cpp' Then you will get a 'main() already has a body' error. You can only have one main function in your app. – lakeweb Sep 26 '16 at 00:02
  • @AdrianColomitchi you said I needed to rename the "main" functions in GCP.cpp and palindrome.cpp, which I did. How do I avoid the troubles this causes now? I read the article, but not really understanding what the information helps me do. – T. Murray Sep 26 '16 at 00:13
  • You will need to compile first the 3 .cpp files: use `g++ -c -Wall .cpp`. This will get you 3 `.o` files - those are the object files - and this phase is called `compilation`. Then you'll need to enter the `linking stage`, which will take the 3 object files and combine it in an executable: `g++ -o MyApp main.o GCD.o palindrome.o` Read the linked material again. Google for "compilation stage C++", [here's one of the results](http://faculty.cs.niu.edu/~mcmahon/CS241/Notes/compile.html) – Adrian Colomitchi Sep 26 '16 at 00:29

1 Answers1

1

In the header you define

int euclid (int, int);

but in the .cpp file you implement

int euclid (int*, int*);

so when you invoke it as gcd(a, b) the linker won't find the integer version. Get rid of the pointers.

PS: In the switch you are invoking main(), you need instead to call palindrome():

switch (choice)
{
    case 1:
        cout << "Palindrome Program.";
        // main();
        palindrome();
vsoftco
  • 55,410
  • 12
  • 139
  • 252