问题
I've looked around and seen quite a few of these, but none of them provided the solution for my problem. I'm getting this compilation error with the following code:
THE ERROR:
THE CODE:
const int TOP_WORDS = 25;
...
void topWords(Hash t, string word, string topA[]);
int main()
{
...
Hash table1;
string word = "example";
string topWordsArr[TOP_WORDS];
table1.addItem(word);
topWords(table1, word, topWordsArr);
...
}
...
void topWords(Hash t, string word, string topA[])
{
int i = 0;
int tempCount = t.itemCount(word);
int tempCount2 = t.itemCount(topA[i]);
while (tempCount > tempCount2 && i < TOP_WORDS) {
i++;
tempCount2 = t.itemCount(topA[i]);
}
if (i > 0)
All the other posts I've seen about this error involved an incorrect syntax with declaring/passing the string array parameter, but I've double and triple checked it all and I'm certain it's correct; though I've been wrong before..
回答1:
Using my crystal ball:
- you're passing the
Hash
by value - this requires the copy constructor,
- you don't have one (or it's botched, private or explicit)
So, take the Hash
by reference
void topWords(Hash const& t, std::string const& word, std::string* topA);
Also,
string[]
is not a type in C++- don't use
using namespace std;
- don't use raw arrays; use
std::vector<std::string>
(orstd::array<std::string, N>
)
来源:https://stackoverflow.com/questions/27114651/error-initializing-argument-1-of