CompSci: Undefined reference to

In summary: Remember to carefully check for errors and use the appropriate data structures to build your expression tree.
  • #1
subwaybusker
51
0

Homework Statement



I'm compiling ExprTree.cpp and it's giving me an Undefined reference to 'BinaryTree::BinaryTree(std::string)', 'BinaryTree::toString()', 'BinaryTree::~BinaryTree()', and an undefined reference to '_WinMain@16'
ExprTree inherits from BinaryTree

Homework Equations



This is my code:


BinaryTree.cpp
Code:
/* BinaryTree.cpp
 *
 */

#include <iostream>
#include "BinaryTree.h"

BinaryTree::BinaryTree() {
	left = 0;
	right = 0;
	this->key = "Default Key";
}

BinaryTree::BinaryTree(string key)
{
	left = 0;
	right = 0;
	this->key = key;
}

BinaryTree::~BinaryTree() {

	if(left != 0)
		delete left;
	if(right != 0)
		delete right;
}

string BinaryTree::getKey()
{
	return key;
}

BinaryTree* BinaryTree::leftChild()
{
	return left;
}

BinaryTree* BinaryTree::rightChild()
{
	return right;
}

void BinaryTree::setLeftChild(BinaryTree* left)
{
	this->left = left;
}

void BinaryTree::setRightChild(BinaryTree* right)
{
	this->right = right;
}

string BinaryTree::toString()
{
	string stringRep = "[" + getKey();

	if(this->left && this->right)
		stringRep += ", " + this->left->toString() + ", " + this->right->toString();
	else if(this->left)
		stringRep += ", " + this->left->toString() + ", []";
	else if(this->right)
		stringRep += ", [], " + this->right->toString();

	return  stringRep + "]";
}


BinaryTree.h
Code:
/* BinaryTree.h
 *
 */

#ifndef BINARYTREE_H_
#define BINARYTREE_H_
#include <string>

using namespace std;

class BinaryTree {
	public:
		BinaryTree();
		BinaryTree(string);
		virtual ~BinaryTree();

		BinaryTree* leftChild();
		BinaryTree* rightChild();
		string getKey();

		virtual string toString();

		void setLeftChild(BinaryTree*);
		void setRightChild(BinaryTree*);

	protected:
		BinaryTree *left;
		BinaryTree *right;

		string key;
};

#endif /* BINARYTREE_H_ */

ExprTree.h
Code:
/* ExprTree.h
 *
 */

#ifndef EXPRTREE_H_
#define EXPRTREE_H_
#include "BinaryTree.h"

class ExprTree:public BinaryTree
{
	public:
        ExprTree(string key);
        virtual ~ExprTree()
        {
            if(left != 0)
                delete left;
            if(right != 0)
                delete right;
        }
		virtual double evaluate() = 0;
};

class OperatorTree:public ExprTree
{
	public:
		OperatorTree(string);
		virtual ~OperatorTree();

		virtual double evaluate();
};

class ValueTree:public ExprTree
{
	public:
		ValueTree(string, int);
		virtual ~ValueTree();

		virtual double evaluate();

	protected:
		int value;
};

#endif /* EXPRTREE_H_ */


ExprTree.cpp
Code:
/* ExprTree.cpp
 *
 */

#include <iostream>
#include "ExprTree.h"
#include "BinaryTree.h"

ExprTree::ExprTree(string key): BinaryTree(key)
{
}

Code:
int main()
{

    string key = "hello";
    BinaryTree *ptr = new BinaryTree(key);
    delete ptr;
    cout << "BinaryTree b" << endl;

}

The Attempt at a Solution



Can anyone also give me hints as to how to make an expression tree? I'm having lots of trouble. Do I need to use a stack? What do I do with the OperatorTree's children if they are not constants but instead operators? I don't know how to use the getkey function from the BinaryTree since Operator isn't directly it's child class.
 
Last edited:
Physics news on Phys.org
  • #2


It seems like you are having trouble with undefined references in your code. This can happen when there are errors in your code, such as missing function definitions or incorrect function calls. It is important to carefully check your code and make sure all functions are properly defined and called in the correct places.

As for creating an expression tree, it is indeed common to use a stack data structure to build and evaluate the tree. You can add nodes to the stack as you traverse through the expression and pop them off when needed to create the appropriate tree structure.

In terms of using the getKey() function from the BinaryTree class in the OperatorTree class, you can still access it by using the "this" keyword, which refers to the current object. For example, you can use "this->getKey()" to get the key of the current OperatorTree object.

I hope this helps and good luck with your code!
 
  • #3




It appears that the issue lies in the implementation of the BinaryTree class and its subclasses. The undefined reference errors suggest that the compiler is unable to find the definitions for the constructor, destructor, and toString() function of the BinaryTree class. This could be due to a variety of reasons, such as incorrect function declarations or missing function definitions in the source files.

To solve this issue, you should carefully check your code and make sure that all function declarations are correct and that the definitions are present in the source files. Additionally, make sure that you have included all necessary header files in the correct locations.

As for creating an expression tree, there are a few different approaches you could take. One way is to use a stack to store the operands and operators as you traverse the expression string. As you encounter an operator, you can pop the top two operands from the stack, create an OperatorTree with the operator as the key and the operands as its children, and push the OperatorTree back onto the stack. This way, the stack will eventually contain a single ExprTree representing the entire expression.

Alternatively, you could use recursion to build the expression tree. Starting from the root node, you can recursively create OperatorTrees and ValueTrees as needed, using the operands and operators in the expression string. This approach may be a bit more complex, but it can be more efficient than using a stack.

As for the issue with getting the key from an OperatorTree, you can still use the getKey() function from the BinaryTree class. Since the OperatorTree class inherits from the BinaryTree class, it will have access to all of its public functions, including getKey().

I hope this helps. Good luck with your coding!
 

What does "Undefined reference to" mean in CompSci?

"Undefined reference to" is an error message that can occur during the compilation of a computer program. It indicates that the compiler was unable to find a definition for a variable, function, or object that was referenced in the program.

Why does "Undefined reference to" occur?

This error can occur for a few reasons, including typos or misspellings in the code, missing or incorrect header files, or missing libraries that contain the definition of the referenced variable or function.

How can I fix "Undefined reference to" errors?

To fix this error, you will need to carefully review your code and double-check for any typos or missing elements. You may also need to ensure that all necessary header files and libraries are included in your program. If the error persists, you may need to consult with other resources or seek assistance from a more experienced programmer.

Can "Undefined reference to" be prevented?

While it is impossible to completely prevent this error from occurring, there are steps you can take to minimize the chances of encountering it. Good coding practices, such as naming variables and functions clearly and consistently, can help reduce the likelihood of typos or misspellings causing this error. Additionally, regularly testing and debugging your code can help catch and fix these errors before they become a larger issue.

Is "Undefined reference to" a serious error?

While it may not always be a major issue, "Undefined reference to" can potentially cause your program to not compile or run correctly. It is important to address these errors as soon as possible to ensure the proper functioning of your program and to maintain the integrity of your code.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
15
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
9K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
891
  • Engineering and Comp Sci Homework Help
Replies
2
Views
939
  • Engineering and Comp Sci Homework Help
Replies
8
Views
837
  • Engineering and Comp Sci Homework Help
Replies
1
Views
851
  • Programming and Computer Science
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
Back
Top