当指针引用向量时,使用向量函数。 [英] Use functions of vector when it's being referred to by a pointer.

查看:201
本文介绍了当指针引用向量时,使用向量函数。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我向我的向量调用一个函数时,我都会收到错误:不能仅通过返回类型重载函数



I keep getting an error whenever I call a function to my vector: cannot overload functions distinguished by return type alone.

#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.

推荐答案

你需要知道你想要什么,有两种可能性:



1.使用

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

1. Using of
vector<string> nameList;
// ...
nameList.push_back(line);

(不是指针)。

2.使用这样的指针:



(not a pointer).
2. Using a pointer like this:

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





你不能混用这两种方法:)



You can't mix this two methods :)


这篇关于当指针引用向量时,使用向量函数。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆