Click here to Skip to main content
15,914,924 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I keep getting an error whenever I call a function to my vector: cannot overload functions distinguished by return type alone.

C++
#include "stdafx.h"
#include <string>
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>

using namespace std;

vector<string>* nameList; // This stores all the possible names

// Defined later (all of the functions below this comment)
void read();
void select();

int main()
{
	read();
	select();

	system("PAUSE");
    return 0;
}

// Reads all the names from the doc. and adds them to a name vector
void read()
{
	fstream names; // Creats file i/o stream
	string line; // Temporary Line that gets cycled

	names.open("fnames.txt"); // Tells file stream to direct itself to the names document

	while(getline(names, line))
	{
		nameList.push_back(line);
	}

	names.close(); // Ends the file stream
}


// Randomly selects a name
string select()
{
	srand(time(NULL)); // Seeds the random number generator by the time

	cout << *nameList[rand() % nameList.size()] << endl; // Prints a value of the name selected to the index of random modulus max index (nameList[rand % maxIndex])
}


What I have tried:

Passing the vector as a parameter.
Posted
Updated 19-May-16 1:13am

1 solution

You need to deside what do you want, there are two possibilities:

1. Using of
C++
vector<string> nameList;
// ...
nameList.push_back(line);
(not a pointer).
2. Using a pointer like this:

C++
vector<string>* nameList;
nameList = new vector<string>();
// ...
nameList->push_back(line);
// ...
delete nameList;


You can't mix this two methods :)
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900