I've been attempting to compile my program to test a few functions out (mind you, it's incomplete) and I've been getting a TON of errors like:
"undefined reference to 'stringArray::func_name(blahblah)'
I can't find what's causing these compiler errors. This is only my second semester with programming and it hasn't happened to me before when I've made header files and implementation files.
#include <iostream>
#include <cstdlib>
#include <cstring>
#include "stringArray.h"
// Constructor
stringArray::stringArray (const size_t max_size)
{
count = 0;
capacity = max_size;
// Dynamically creates space for strings in the bag
data = new stringArray::str[max_size];
}
// Copy constructor
stringArray::stringArray (const stringArray& source)
{
count = source.count;
capcity = source.capacity;
// Maybe call the overload function (=) for a DEEP copy? (TO BE DONE)
// std::copy(source.data, source.data + source.count, data);
for (int i = 0; i < capacity; i++)
{
data[i] = source.data[i];
}
}
// Destructor
stringArray::~stringArray();
{
// Frees up memory
// Uses a for loop to delete each element in the bag
for (int j = 0; j < capacity; j++)
{
delete[] data[j];
}
// Finally deletes data itself
delete data;
}
// Adds a string into the bag
// Returns true if successful; false otherwise
bool stringArray::addString(constchar new_string[ ])
{
// Check bounds to see if the bag is filled
// If it attempts to add the string into the full bag, return false
if ((count + 1) > capacity)
{
returnfalse;
}
// Otherwise, return true and add the string into the bag
else
{
// Adds the string into the bag
data[count] = new_string[];
// Increases the number of items in the bag
count++;
returntrue;
}
}
// Removes one occurrence of old_string from the bag
// Returns true if successful; false otherwise
bool stringArray::removeString(constchar old_string[ ]);
{
// Searches for the string
for (int i = 0; i < capacity; i++)
{
if (data[i] == old_string[])
{
// Shift all of the elements to the left
for (int index = i; index < capacity; index++)
{
data[index] = data[index+1];
}
// Reduces count by 1
count = count - 1;
returntrue;
}
}
// Returns false if the string cannot be found/removed
returnfalse;
}
// Removes all occurrences of old_string from the bag
// Returns number of strings removed
size_t stringArray::removeAll(constchar old_string[ ])
{
// Counter variable to return at the end
size_t counterRemoved = 0;
// Searches through the object for the string
for (int i = 0; i < capacity; i++)
{
if (data[i] == old_string[])
{
// Shift all of the elements to the left
for (int index = i; index < capacity; index++)
{
data[index] = data[index+1];
}
// Reduces count by 1 and increments the counter
count = count - 1;
counterRemoved = counterRemoved + 1;
}
}
return counterRemoved;
}
// Substitutes/replaces ONE occurrence of old_string with new_string
// Returns true if successful; false otherwise
bool stringArray::substitute(constchar old_string[ ], constchar new_string[ ])
{
// Searches through the object
for (int i = 0; i < capacity; i++)
{
if (data[i] == old_string[])
{
// Replace the old_string[] with the new_string[]
data[i] = new_string[];
// Returns true since the string has been replaced
returntrue;
}
}
returnfalse;
}
// Substitutes/replaces ALL occurrences of old_string with new_string
// Returns number of strings replaced
size_t stringArray::substituteAll(constchar old_string[ ], constchar new_string[ ])
{
// Counter variable
size_t count = 0;
// Searches through the object
for (int i = 0; i < capacity; i++)
{
if (data[i] == old_string[])
{
// Replaces and increments count
data[i] = new_string[];
count++;
}
}
// Returns the number of strings replaced
return count;
}
// Returns total number of strings in the bag
size_t stringArray::size( ) const
{
return count;
}
// Returns true if cur_string is in the bag; false if not
bool stringArray::is_string(constchar cur_string[ ]) const
{
// Searches through the object
for (int i = 0; i < capacity; i++)
{
if (data[i] == cur_string[])
{
// Returns true if the string is found in the object
returntrue;
}
}
returnfalse;
}
// Returns number of occurrences of cur_string
size_t stringArray::occurrences(constchar cur_string[ ]) const
{
// Counter to determine how many occurrences to return
size_t counter = 0;
// Searches through the object
for (int i = 0; i < capacity; i++)
{
if (data[i] == cur_string[])
{
// Increment the counter by 1
counter = counter + 1;
}
}
return counter;
}
// Prints out all strings in the object
// One string per line
void stringArray::printStringArray( ) const
{
for (int i = 0; i < capacity; i++)
{
cout << data[i] << "\n";
}
}
// Prints all strings out in the object
// Sorted order from least to greatest
void stringArray::printSortedStringArray( ) const
{
// TO BE DONE
}
#ifndef STRINGARRAY_H_INCLUDED
#define STRINGARRAY_H_INCLUDED
class stringArray
{
public:
// Constructor and copy constructor
stringArray (const size_t max_size = DEFAULT_SIZE);
stringArray (const stringArray& source);
// Destructor
~stringArray();
// Adds a new string in the bag
// Returns true if successful; false otherwise
bool addString(constchar new_string[ ]);
// Removes one occurrence of old_string from the bag
// Returns true if successful; false otherwise
bool removeString(constchar old_string[ ]);
// Removes all occurrences of old_string from the bag
// Returns number of strings removed
// May need to be changed to an int
size_t removeAll(constchar old_string[ ]);
// Substitutes/replaces one occurrence of old_string with new_string
// Returns true if successful; false otherwise
bool substitute(constchar old_string[ ], constchar new_string[ ]);
// Substitutes/replaces all occurrences of old_string with new_string
// Returns number of strings replaced
size_t substituteAll(constchar old_string[ ], constchar new_string[ ]);
// Returns total number of strings in the bag
size_t size( ) const;
// Returns true of cur_string is in the bag; false if not
bool is_string(constchar cur_string[ ]) const;
// Returns number of occurrences of cur_string
size_t occurrences(constchar cur_string[ ]) const;
// Prints out all strings in the object
// One string per line
void printStringArray( ) const;
// Prints all strings out in the object
// One string per line
// Sorted order from least to greatest
void printSortedStringArray( ) const;
// Sets the default_size/capacity of the bag to 20
staticconst size_t DEFAULT_SIZE = 20;
// Typedef stuff
typedefchar* str;
private:
// count = The number of items
size_t count;
// capacity = The number of items that can be stored
size_t capacity;
// data = Item
str* data;
};
#endif // STRINGARRAY_H_INCLUDED
I tried compiling this code and got all sorts of errors. Many of which you should be able to figure out on your own.
But here we go!
stringArray.h
line 57: you're not const correct. you probably want to change this typedef to be constchar* instead of char*. Although you have deeper problems. See below.
stringArray.cpp
line 21: you misspelled capacity
line 35: you shouldn't have a semicolon there
line 66: you can't have empty brakets like that. Just = new_string;
line 79: shouldn't have a semicolon
line 84: again with the empty brakets. You can't have empty brakets.
line 114: ditto
line 140: again
line 143: "
line 165, 168, 193: more of the same
line 209: This line makes no sense. Is that + supposed to be a =?
line 214: empty brakets
line 232: you're using cout without being in std namespace. Either use std::cout or put in using namespace std
Thanks for replying though. I'm confused as to why line 57 in stringArray.h needs to be a const char* and not just char*.
Because of this:
1 2 3 4 5
bool stringArray::addString(constchar new_string[ ]) // <- new_string is const
{
//...
data[count] = new_string; // <- can't convert a const pointer to a nonconst pointer
// therefore, data must be a const pointer
We're not allowed to use strings for this assignment (Which is dumb).
I agree.
Fixed all of those problems though, but I'm still getting "Undefined Reference" errors...
I'm not. Once I fix all those problems it compiles and links fine (although I do get some warnings about comparing signed/unsigned types)
It would help if you posted the full error message instead of just summarizing it.
Line 9 - undefined reference to 'stringArray::stringArray(unsigned int)'
Line 9 - undefined reference to 'stringArray::stringArray(unsigned int)'
Line 10 - undefined reference to 'stringArray::addString(char const*)'
Line 11 - undefined reference to 'stringArray::addString(char const*)'
Line 12 - undefined reference to 'stringArray::printStringArray() const'
Line 13 - undefined reference to 'stringArray::addString(char const*)'
Line 15 - undefined reference to 'stringArray::~stringArray()'
Line 15 - undefined reference to 'stringArray::~stringArray()'
Line 15 - undefined reference to 'stringArray::~stringArray()'
Line 15 - undefined reference to 'stringArray::~stringArray()'
Sometimes, I think it's my compiler that's hating me and causing all of these errors, but at the same time...I really don't know.
I really don't think you are. I suspect you didn't check the "add file to active project" checkbox or the "debug/release" checkboxes (which are strangely unchekced by default in C::B) when you created that cpp file.
In the project manager in the left column thing:
- right click on stringArray.cpp
- go to properties.
- go in the "Build" tab
- make sure "Compile File" and "Link File" are both checked
- make sure the "Debug" and "Release" boxes in "Belongs in Targets" are both checked