hi everyone,

I'm working on a project for one of my classes that parses a given grammar. I'm pretty sure I have coded it correctly but I keep getting the following list of linker errors:
[Linker error] undefined reference to `assign(char)'
[Linker error] undefined reference to `integer(char)'
[Linker error] undefined reference to `literal(char)'
[Linker error] undefined reference to `expr(char)'
[Linker error] undefined reference to `primary(char)'
[Linker error] undefined reference to `term(char)'
[Linker error] undefined reference to `term(char)'
[Linker error] undefined reference to `expr(char)'
[Linker error] undefined reference to `integer(char)'
[Linker error] undefined reference to `expr(char)'
ld returned 1 exit status

If anyone has any idea why this is happening or what I can do to fix it I would appreciate it immensely !!!! Thanks in advance : )


Here is my code:

#include <iostream>
#include <fstream>
#include <string>


using namespace std;

bool assign(char);
bool expr(char);
bool term(char);
bool primary(char);
bool integer(char);
bool literal(char);

int main()
{

    ifstream inFile;
    inFile.open("parser.txt");

    
    string stringToTest;
    
    inFile >> stringToTest;
    
    char *c;
    *c = stringToTest[0];
    
    cout << "c : " << c << endl;
    
    if (assign(*c) && *c == '\0')
       cout << "The string: " << stringToTest << "is in the language" << endl;
       else
       cout << "The string: " << stringToTest << "is not in the language" << endl;
    
    inFile.close();
    system("Pause");
    return 0;
}

bool literal(char *c)
{
     cout << "in literal" << endl;
     if (*c == 0 || *c == 1 || *c == 2 || *c == 3 || *c == 4 || *c == 5 || *c == 6 || *c == 7 || *c == 8 || *c == 9)
     {   
        ++c;
        return true;
     }
     else
         return false;
}

bool integer (char *c)
{
     cout <<"in integer" << endl;
     if (*c == 'a' || *c == 'b' || *c == 'c' || *c == 'd' || *c == 'e' || *c == 'f' || *c == 'g' || *c == 'h' || *c == 'i' || *c == 'j' || *c == 'k' || *c == 'l' || *c == 'm' || *c == 'n' || *c == 'o' || *c == 'p' || *c == 'q' || *c == 'r' || *c == 's' || *c == 't' || *c == 'u' || *c == 'v' || *c == 'w' || *c == 'x' || *c == 'y' || *c == 'z')
        {
        ++c;
        return true;
        }
     else
         return false;
}

bool primary (char *c)
{
     cout << "in primary" << endl;
     if(integer(*c))
        return true;
     else if (literal(*c))
          return true;
     else if (*c == '(')
          ++c;
          if (expr(*c))
             if (*c == ')')
             {
                ++c;
                return true;
             }
             else
                 return false;
}

bool term (char *c)
{
     cout << "in term" << endl;
     if (primary(*c))
     {
          ++c;
          if (*c == '*' || *c == '/')
             ++c;
             if (term(*c))
             {
                ++c;
                return true;
             }
             else
                 return false;
     }
     else
         return false;
}

bool expr (char *c)
{
     cout << "in expression" << endl;
     if (term(*c))
     {
          if (*c == '+' || *c == '-')
             ++c;
             if (*c == expr(*c))
                return true;
             else
                 return false;
     }
     else
         return false;
}

bool assign (char *c)
{
     cout << "in assign" << endl;
     if (integer(*c))
     {
          if (*c == '=')
             ++c;
             if (expr(*c))
                return true;
             else
                 return false;
     }
     else
         return false;
}

char and char* are two different types. You should audit your code looking for mistakes with pointers, because I see more than just that mismatch in terms of pointer problems.

Edited 7 Years Ago by Tom Gunn: n/a

char and char* are two different types. You should audit your code looking for mistakes with pointers, because I see more than just that mismatch in terms of pointer problems.

I'll admit, pointers confuse me. Can this be done without pointers? I'm not sure. To keep the pointers, I'm guessing above main all the function declarations should be of the form bool assign (char*); ?

Lots of problems only in pointers.

1) Yes you guessed it right the declarations should be of form <function>(char *) format.
2) Consider the following example:

void function(char *a)
{
    // Function Definition
}

// Somewhere in main

char arr[20],a;
char *p;
p=&a;

// Call can be either like
function( arr );
// Or as
function( p );

the criteria is that to a pointer variable you pass the address which it should hold and operate upon.Major flaws with regard to this concept and not the value that the pointer holds as you have colled function(*p) and function(*arr) .

3)Consider the following :

char *p;
// A pointer is created which doesn't point to any memory address.
*p='a';
//trying to put a value into the address pointed to by pointer 
// results in a error as pointer doesn't point to any memory address.

Proper code is:

char *p,a;
p=&a;
*p='f';

Edited 7 Years Ago by csurfer: n/a

This article has been dead for over six months. Start a new discussion instead.