简体   繁体   English

具有结构向量作为返回值的C ++函数

[英]C++ function with a struct vector as a return value

I want to create a private function in C++ (VS2010). 我想在C ++(VS2010)中创建一个私有函数。 It should return a vector/array of structs/userdefined type. 它应该返回一个向量/结构/用户定义类型的数组。

However I think my declaration of the function in the cpp file is perhaps wrong. 但是我认为我在cpp文件中声明该功能可能是错误的。 Or maybe already in the header. 也许已经在标题中了。 Can somebody have a look at it? 有人可以看看吗?

My header looks like this: 我的标题看起来像这样:

#pragma once
using namespace std;
#include <algorithm>
#include <vector>
class clsWString2
{
private:
struct udtWChar2
{
    wstring Text;
    int OrigPos;
};
bool m_bDirty;
vector<udtWChar2>pToWChar2(wstring u);

vector<udtWChar2>m;
public:
clsWString2(void);
~clsWString2(void);
void ReplaceCompareBinary(wstring uSearchFor, wstring uReplaceBy);
void ReplaceCompareText(wstring uSearchFor,wstring uReplaceBy);
void ReplaceByPos(int uStartPos1Based,int uLen0Based, wstring uReplaceBy);
void FeedString(wstring u);
void Append(wstring u);
wstring CharAtPos(int uIndex);
int OrigPos(int uIndex);
};

And my .cpp file looks like this: 我的.cpp文件看起来像这样:

#include "StdAfx.h"
#include "clsWString2.h"


clsWString2::clsWString2(void)
{
m.resize(0);
}
clsWString2::~clsWString2(void)
{
}
vector<udtWChar2> clsWString2::pToWChar2(wstring u)
{
vector<udtWChar2> n;
n.resize(0);

for (int i=0;i<u.size();i++)
{

    wstring sChar;
    sChar=u.substr(i,1);

    udtWChar2 nc;
    nc.Text =sChar;
    nc.OrigPos=i;

    n.push_back (nc);
}

return n;
}

In the source file, when you define the function, the return type is not in the scope of the class so the class in the vector needs to be fully qualified: 在源文件中,当您定义函数时,返回类型不在类的范围内,因此向量中的类需要完全限定:

vector<clsWString2::udtWChar2> clsWString2::pToWChar2(wstring u)
{
    ...
}

啊,我明白了:

vector<clsWString2::udtWChar2> clsWString2::pToWChar2(wstring u)

You can not use directly udtWChar2 as type , you need define type def or you need use as struct udtWChar2 您不能直接将udtWChar2用作类型,需要定义类型def或将其用作结构udtWChar2

Like : 喜欢 :

in .h 在.h中

vector  < struct udtWChar2 > pToWChar2(wstring u);
vector  < struct udtWChar2 > m;

in .cpp 在.cpp中

vector < struct clsWString2::udtWChar2 > clsWString2::pToWChar2(wstring u)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM