In my program, I am trying to create a program that places randomly generated integers in to a vector and then bubble sorts them. When I try to build the file I get the following error:
"Error LNK2019 unresolved external symbol "private: class std::vector<int,class std::allocator<int> > __thiscall SortedVector::listOfNums(unsigned int)" (?listOfNums@SortedVector@@AAE?AV?$vector@HV?$allocator@H@std@@@std@@I@Z) referenced in function "public: void __thiscall SortedVector::bubbleSortVector(void)" (?bubbleSortVector@SortedVector@@QAEXXZ) Test1 C:\Users\Darien\Documents\C++ files\Test1\Test1\main.obj"
Does anyone know what external symbol is causing this problem? I don't have any user created header files.
#include "pch.h"
#include <iostream>
#include <vector>
#include <ctime>
class SortedVector
{
public:
SortedVector(size_t vectorSize = 100)
: listOfNums(vectorSize) { };
void placeRandNumsInVector() {
for (unsignedint i = 0; i < listOfNums.size(); i++) {
int randomNumber = rand() % 1000 + 1;
listOfNums[i] = randomNumber;
std::cout << listOfNums[i] << " ";
}
}
void bubbleSortVector() {
bool whileLoopEnd = false;
while (whileLoopEnd == false) {
bool forLoopEnd = true;
for (unsignedint i = 0; i < listOfNums.size(); i++) {
//if loop is on final iteration, then listOfNums does not compare its elements to each other
if (i != (listOfNums.size() - 1)) {
if (listOfNums[i] > listOfNums[i + 1]) {
//listOfNums[i] and listOfNums[i + 1] swap values
int tempVar = listOfNums[i + 1];
listOfNums[i + 1] = listOfNums[i];
listOfNums[i] = tempVar;
forLoopEnd = false;
}
}
//if no values were swapped, then while loop ends and sorting ends
elseif (i == (listOfNums.size() - 1)) {
if (forLoopEnd == true) {
whileLoopEnd = true;
}
}
}
}
}
void printVectorValues() {
for (unsignedint i = 0; i < listOfNums.size(); i++) {
std::cout << listOfNums[i] << " ";
}
}
private:
std::vector<int> listOfNums;
};
int main()
{
//determines seed based on the time of program execution
srand( (unsignedint)time(NULL));
//instantiates instance of SortedArray
SortedVector sortedVector;
std::cout << "initial unsorted array values: ";
////for loop assigns each vector location a random integer
sortedVector.placeRandNumsInVector();
std::cout << std::endl;
std::cout << "Integers are now placed in array" << std::endl;
std::cout << std::endl;
std::cout << "Program will now sort integers in listOfNums from least to greatest" << std::endl;
std::cout << std::endl;
//Sorts integers in listOfNums from least to greatest
sortedVector.bubbleSortVector();
std::cout << "Sorted vector: ";
sortedVector.printVectorValues();
return 0;
};
When I get rid of "pch.h" I get this error message:
"Error C1010 unexpected end of file while looking for precompiled header. Did you forget to add '#include "pch.h"' to your source?"
I should have added I am using Visual Studio IDE. I suppose I can also just compile and run it in Power Shell as well.
// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
#ifndef PCH_H
#define PCH_H
// TODO: add headers that you want to pre-compile here
#endif //PCH_H
So there's nothing in it, really.
Then there's nothing wrong with your program and the error makes no sense relating to the code you've shown.
In fact, it seems to be related to your previous question (and therefore previous code).