【发布时间】:2012-03-09 20:36:33
【问题描述】:
我正在尝试使用建议的函数 here 通过分隔符拆分字符串,但每当我尝试使用 vector<string> 作为返回类型时都会遇到一些错误。
我做了一个简单的函数,它返回一个vector<string> 作为测试,但我仍然得到同样的错误:
// Test.h
#pragma once
#include <vector>
#include <string>
using namespace std;
using namespace System;
namespace Test
{
vector<string> TestFunction(string one, string two);
}
.
//Test.cpp
#include "stdafx.h"
#include "Test.h"
namespace Test
{
vector<string> TestFunction(string one, string two) {
vector<string> thing(one, two);
return thing;
}
}
还有错误截图:
有谁知道为什么我似乎无法使用 vector<string> 作为返回类型?
【问题讨论】:
-
您的返回类型必须是
std::vector<std::string> -
@e.James
string和vector都在std命名空间中,对吧?所以不应该using namespace std;照顾吗? -
旁白:在 .h 文件中包含
using通常被认为是非常糟糕的做法。尤其是引用命名空间与类型的一种 -
vector<string> thing(one, two);这不会用两个字符串初始化向量,没有这样的构造函数。 -
@WilHall:是的,我的错。我没有意识到标题中有
using指令。可怕!
标签: c++ string vector return-type