0

I need to provide a CFG class in a separate file, but I'm unsure how to compile it together with the associated .h and the main program.

I've #includeed the .h file and I've asked for both files at the command line, but I'm not sure why this is wrong for compiling them together.

Thoughts?

CFG.cpp:

#include <iostream>
#include <stdio.h>
#include <string>

using namespace std;

class CFG
{
    public:
        string code[25];
        char startNT;
    //private:

    CFG(string inCode[], int stringLen)
    {
        for (int a = 0; a < stringLen; a++)
        {
            //cout << inCode[a] << endl;
            this->code[a] = inCode[a];
        }
        for (int a = 0; a < stringLen; a++)
        {
            cout << this->code[a] << endl;
        }
    }

    char getStartNT()
    {
        return startNT;
    }

    void setStartNT(char stNT)
    {
        startNT = stNT;
    }

    bool processData(string inString, string wkString)
    {
        //Our recursive function
        return true;
    }

    void garbage()
    {
        return;
    }
};

CFG.h:

#ifndef _cfg_h_
#define _cfg_h_

#include <iostream>
#include <stdio.h>
#include <string>

using namespace std;

class CFG
{
    public:
        string code[25];
        char startNT;

        CFG(string inCode[], int stringLen);
        char getStartNT();
        void setStartNT(char stNT);
        bool ProcessData(string inString, string wkString);
        void garbage();
};

#endif

cfg_entry.cpp:

#include <stdio.h>
#include <iostream>
#include "cfg.h"

using namespace std;

int main()
{
    string inArray[5];
    inArray[0] = "test0";
    inArray[1] = "test1";
    inArray[2] = "test2";
    inArray[3] = "test3";
    inArray[4] = "test4";
    CFG * cfg1 = new CFG(inArray, 5);
    cfg1->garbage();
    return 0;
}

Compile errors:

art@tv:~/Dropbox/Weber/CS 4110/Individual Assignment 2$ g++ -g -std=c++11 -Wall -o cfg_entry cfg.cpp cfg_entry.cpp
/tmp/ccICQEd0.o: In function `main':
/home/art/Dropbox/Weber/CS 4110/Individual Assignment 2/cfg_entry.cpp:15: undefined reference to `CFG::CFG(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*, int)'
/home/art/Dropbox/Weber/CS 4110/Individual Assignment 2/cfg_entry.cpp:16: undefined reference to `CFG::garbage()'
collect2: error: ld returned 1 exit status
UtahJarhead
  • 2,091
  • 1
  • 14
  • 21
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Ken White Oct 17 '17 at 01:35
  • 1
    You re-implemented your entire class in the .cpp file. That is not how you implement the functions.. Just `returnType className::func(parameterType parameter...) {..body..}` for each function in your class. Note: Constructors and Destructors do not have return types. – Brandon Oct 17 '17 at 01:42
  • The class definition in the .cpp file is a not-allowed *redefinition* of the class. You don't need to define the class again. Just define the member functions. – Cheers and hth. - Alf Oct 17 '17 at 01:43
  • In other news: in C++ it's a good idea to reserve ALL UPPERCASE for macros. – Cheers and hth. - Alf Oct 17 '17 at 01:44
  • Example: https://ideone.com/nOG2aC – Brandon Oct 17 '17 at 01:49
  • I understand! I thought the .h was just to give the compiler a heads up as to what was included in the .cpp. I was obviously wrong. – UtahJarhead Oct 17 '17 at 01:51

1 Answers1

1

I found my issue. In my case, the header file was defining the class and the .cpp file was re-defining it again, trying to create 2 instances of the CFG class. The .h needed to handle the class declaration and variable instantiation while the .cpp handles only the function definitions.

cfg.h:

#ifndef _cfg_h_
#define _cfg_h_

#include <iostream>
#include <stdio.h>
#include <string>

using namespace std;

class CFG
{
    private:
        string code[25];
        char startNT;

    public:
        CFG(string inCode[], int stringLen);
        char getStartNT();
        void setStartNT(char stNT);
        bool processData(string inString, string wkString);
        void garbage();
};

#endif

cfg.cpp:

#include <iostream>
#include <stdio.h>
#include <string>
#include "cfg.h"

using namespace std;

CFG::CFG(string inCode[], int stringLen)
{
    for (int a = 0; a < stringLen; a++)
    {
        //cout << inCode[a] << endl;
        this->code[a] = inCode[a];
    }
    for (int a = 0; a < stringLen; a++)
    {
        cout << this->code[a] << endl;
    }
}

char CFG::getStartNT()
{
    return startNT;
}

void CFG::setStartNT(char stNT)
{
    startNT = stNT;
}

bool CFG::processData(string inString, string wkString)
{
    //Our recursive function
    return true;
}

void CFG::garbage()
{
    return;
}
UtahJarhead
  • 2,091
  • 1
  • 14
  • 21