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
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;
}
Related
I'm trying to use the DependencyCollector class of Clang in my Tool to list all the dependencies in a file, lets say test.cpp
Here is my program:
#include "stdafx.h"
#include <iostream>
#include "clang/Frontend/FrontendActions.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/Support/CommandLine.h"
#include "clang/Lex/PPCallbacks.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/Utils.h"
using namespace std;
using namespace clang::tooling;
using namespace clang;
using namespace llvm;
static cl::OptionCategory MyToolCategory("my-tool options");
static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
static cl::extrahelp MoreHelp("\nMore help text...");
class myDependencyCollector : public DependencyCollector {
private:
public:
bool sawDependency(StringRef Filename, bool FromModule, bool IsSystem, bool IsModuleFile, bool IsMissing) {
if (Filename == "stdafx.h" || IsSystem) {
return false;
} else {
return true;
}
}
bool needSystemDependencies() {
return false;
}
};
class DependencyAction : public PreprocessOnlyAction {
private:
myDependencyCollector *col;
public:
virtual bool usesPreprocessOnly() const {
return true;
}
bool BeginSourceFileAction(CompilerInstance &ci) {
Preprocessor &pp = ci.getPreprocessor();
col = new myDependencyCollector();
col->attachToPreprocessor(pp);
return true;
}
void ExecuteAction() {
}
virtual void EndSourceFileAction() {
llvm::ArrayRef<string> arr = col->getDependencies();
int size = arr.size();
for (int i = 0; i < size; i = i+1) {
cout << arr[i] << endl;
}
}
};
int main(int argc, const char **argv)
{
CommonOptionsParser OptionsParser(argc, argv, MyToolCategory);
ClangTool Tool(OptionsParser.getCompilations(), OptionsParser.getSourcePathList());
int result = Tool.run(newFrontendActionFactory<DependencyAction>().get());
return result;
}
Now, if I run the program on, for example the file test.cpp:
#include <iostream>
#include "test.h"
void do_math(int *x) {
*x += 5;
}
int main(void) {
int result = -1, val = 4;
do_math(&val);
return result;
}
The program doesn't find any includes.
It would be great if anybody could help me because I have not been able to find an answer after hours of searching on the internet.
The problem is that you overwrite the ExecuteAction() method from class PreprocessOnlyAction with an empty body.
If you delete the line:
void ExecuteAction() {}
everything works as expected.
Hi I am working on a program that involves a Main.cpp, Connect4.cpp, and Connect4.h file. When I compile my program I am getting an error in the Main file saying that my playGame function is an undefined reference. I am compiling both files together(main first) I believe something is wrong in the way I am trying to dynamically call the function playGame. Any input would be much appreciated!
Main.cpp
#include <iostream>
#include <array>
#include "Connect4.h"
void playGame();
using namespace std;
int main()
{
Connect4 *ptr;
ptr=new Connect4;
ptr-> playGame();
delete ptr;
}
Connect4.cpp
#include <iostream>
#include <array>
#include "Connect4.h"
char gameBoard[9][7];
int rows;
int columns;
using namespace std;
void playGame()
{
void display();
int selectColumn(bool);
int tokenPlacement(char token, int columns);
bool winOrLose();
cout<<"Welcome to Connect Four.";
for(int i=0; i<rows;++i)
{
for(int j=0; j<columns; ++j)
{
gameBoard[i][j]=' ';
}
}
bool player1Turn=true;
char winner='n';
int column =0;
while(true){
display();
column=selectColumn(player1Turn);
if(player1Turn==true)
{
tokenPlacement('x',column);
player1Turn=false;
}
else
{
tokenPlacement('o', column);
player1Turn=true;
winner= winOrLose();
if(winner!='n')
{
break;
}
}
cout<<"Winner is:"<<winner;
}
Connect4.h
#ifndef CONNECT4_H_
#define CONNECT4_H_
#include
using namespace std;
class Connect4 {
public:
static void playGame();
private:
void display();
int selectColumn(bool);
int tokenPlacement(char, int);
bool winOrLose();
char gameBoard[9][7];
};
#endif /* CONNECT4_H_ */
I am trying to implement a biginteger class, and after I created a biginteger class, with a proper header file, and at first I am trying to define a operator=() operator, so when I make a new biginteger object, I will be able to make it equals with a integer.
This is the main.cpp:
#include <iostream>
#include "bigint.h"
using namespace std;
int main()
{
bigint bela = 15;
cout << "Hello world!" << bela.mennyi() <<endl;
return 0;
}
And this is the biginteger header:
#ifndef BIGINT_H
#define BIGINT_H
#include <vector>
#include <iostream>
class bigint
{
public:
bigint();
void operator=(const int &a);
int mennyi();
protected:
private:
std::vector<int> numarray;
};
#endif // BIGINT_H
And the biginteger.cpp file:
#include "bigint.h"
#include <iostream>
using namespace std;
bigint::bigint()
{
numarray.resize(0);
}
void bigint::operator=(const int &a)
{
int b = a;
if(b >= 0)
{
numarray.resize(0);
while(b!=0){
numarray.push_back(b%10);
b = b/10;
}
}
}
int bigint::mennyi()
{
int ki = 0;
for(int i = (numarray.size())-1; i>=0; i--)
{
ki = ki*10 + numarray[i];
}
return ki;
}
When I start the debugging I get an error saying: conversion from 'int' to non-scalar type 'bigint' requested.
You should implement this constructor:
bigint::bigint(int);
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 8 years ago.
I'm trying to test a program and every time I go to compile it, I get the error LNK2019: unresolved external symbol "public: __thiscall Prog3Graph::Prog3Graph(void)" (??0Prog3Graph##QAE#XZ) referenced in function _main. I was wondering what is causing this and how I can fix it. I've tried messing around with the code, but can't figure what is causing it.
Prog3Graph.cpp:
#include <iostream>
#include <fstream>
#include <string.h>
#include <stdlib.h>
#include "Prog3Graph.h"
#include "GraphNode.h"
using namespace std;
int main()
{
Prog3Graph *test;
test = new Prog3Graph();
test->buildGraph("Graph.txt");
test->printGraph();
return 0;
}
bool Prog3Graph::buildGraph(char *fileName)
{
int i,j,index,numlinks, link;
char line[24];
ifstream inFile;
inFile.open(fileName, ifstream::in);
if(!inFile.is_open())
{
cout << "Unable to open file " << fileName << ". \nProgram terminating...\n";
return 0;
}
for(i=0;i<10;i++)
{
getNextLine(line,24);
index = atoi(line);
Nodes[i].setNodeID(index);
getNextLine(line,24);
Nodes[i].setNodeData(line);
getNextLine(line,24);
numlinks = atoi(line);
for(j=0;j<numlinks;j++)
{
getNextLine(line,24);
link = atoi(line);
AdjMatrix[i][link]=1;
}
inFile.close();
}
return true;
}
void Prog3Graph::printGraph()
{
int i,j;
cout << "------------------------------------------------------------\n\n";
cout << " Adjacency Matrix:\n\n";
cout << " 0 1 2 3 4 5 6 7 8 9\n";
cout << " +---------------+\n";
for(i=0; i<10; i++)
{
cout << i << "|";
for(j=0; j<10; j++)
{
cout << AdjMatrix[i][j] << "|";
}
cout << "\n +---------------+\n";
}
}
bool Prog3Graph::getNextLine(char *line, int lineLen)
{
int done = false;
ifstream inFile;
while(!done)
{
inFile.getline(line, lineLen);
if(inFile.good())
{
if(strlen(line) == 0)
continue;
else if(line[0] == '#')
continue;
else done = true;
}
else
{
strcpy(line, "");
return false;
}
}
return true;
}
Prog3Graph.h:
#pragma once
#include <iostream>
#include <fstream>
#include <string.h>
#include <stdlib.h>
#include "GraphNode.h"
using namespace std;
class Prog3Graph
{
private:
ifstream inFile; // File stream to read from
int AdjMatrix[10][10];
GraphNode Nodes[10];
public:
Prog3Graph(); // Class constructor
~Prog3Graph(); // Class destructor
bool buildGraph(char *filename); // Read graph file, build graph
void printGraph(); // Print all data in graph
void depthFirstTraversal(); // Perform a depth first traversal
private:
bool getNextLine(char *line, int lineLen); // Read next line from graph file
};
GraphNode.cpp:
#include <iostream>
#include <fstream>
#include <string.h>
#include <stdlib.h>
#include "GraphNode.h"
using namespace std;
void GraphNode::setNodeID(int ID)
{
m_iNodeID = ID;
}
int GraphNode::getNodeID()
{
return m_iNodeID;
}
void GraphNode::setNodeData(char *data)
{
int i;
for(i=0;i<24;i++)
{
m_sNodeData[i] = data[i];
}
}
char *GraphNode::getNodeData()
{
return &m_sNodeData[24];
}
void GraphNode::setVisited(bool visited)
{
m_bVisited = visited;
}
bool GraphNode::hasBeenVisited()
{
return m_bVisited;
}
GraphNode.h:
#pragma once
#include <iostream>
#include <fstream>
#include <string.h>
#include <stdlib.h>
using namespace std;
class GraphNode
{
private:
int m_iNodeID;
char m_sNodeData[24];
bool m_bVisited;
public:
GraphNode();
~GraphNode();
void setNodeID(int ID);
int getNodeID();
void setNodeData(char *data);
char *getNodeData();
void setVisited(bool visited);
bool hasBeenVisited();
};
Read the message carefully:
unresolved external symbol "public: __thiscall Prog3Graph::Prog3Graph(void)" (??0Prog3Graph##QAE#XZ) referenced in function _main.
You've declared a constructor (and a destructor) for your class, but you've never actually defined them.
Im receiving this error when trying to compile my code.
$ g++ -o BangBangControlTest BangBangControl.o BangBangControlTest.o
ld: duplicate symbol _heating_unit in BangBangControlTest.o and BangBangControl.o for architecture x86_64
collect2: ld returned 1 exit status
I am new to C++ and can't find out what is wrong. I've searched through many tutorials and looked at similar error messages received by other stack users. Here are my classes.
"BangBangControlTest.cpp"
// Test function
#include <iostream>
#include "BangBangControl.h"
using namespace std;
int main(){
BangBangControl control(50, true, 75);
for(int i = 0; i < 50; i++){
std::cout << "Temp = " << control.update() << endl;
}
return 0;
}
"BangBangControl.cpp"
#include <iostream>
#include "BangBangControl.h"
using namespace std;
BangBangControl::BangBangControl(int temp, bool isOn, int initialTemp){
heating_unit = HeatingUnit(isOn, initialTemp);
temp_to_maintain = temp;
}
void BangBangControl::setTemp(int temp){temp_to_maintain = temp;}
int BangBangControl::getTemp(){return temp_to_maintain;}
int BangBangControl::update(){
int b=heating_unit.tick();
if (b > temp_to_maintain + 2) heating_unit.turnOff(); if (b < temp_to_maintain - 2) heating_unit.turnOn();
return b;
}
"BangBangControl.h"
// BangBangControl header
#include <iostream>
#include "HeatingUnit.h"
using namespace std;
HeatingUnit heating_unit;
int temp_to_maintain;
class BangBangControl{
public:
BangBangControl(int, bool, int);
void setTemp(int);
int getTemp();
int update();
};
"HeatingUnit.cpp"
// HeatingUnit class implementation
#include <iostream>
#include "HeatingUnit.h"
using namespace std;
HeatingUnit::HeatingUnit(bool a, int b){
isOn = a;
temp = b;
}
void HeatingUnit::turnOn(){isOn = true;}
void HeatingUnit::turnOff(){isOn = false;}
int HeatingUnit::tick(){
if(isOn && temp <= 100){
return ++temp;
}
else if((!isOn) && temp >= 0){
return --temp;
}
else{
return temp;
}
}
"HeatingUnit.h"
#include <iostream>
using namespace std;
class HeatingUnit{
public:
bool isOn;
int temp;
HeatingUnit();
HeatingUnit(bool, int);
void turnOn();
void turnOff();
int tick();
};
You see that HeatingUnit heating_unit; in your header file? You need to put extern in front of it, and copy the original version without the extern to the .cpp file, optionally specifying an initial value there.
You can read more about this here: How do I use extern to share variables between source files?