0

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:

enter image description here

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

John Saunders
  • 160,644
  • 26
  • 247
  • 397
user3776749
  • 667
  • 1
  • 10
  • 20
  • 3
    You're getting *what* compilation error? You only posted half of one. – molbdnilo Nov 24 '14 at 21:40
  • 1
    I can't believe this. The compiler clearly states argument 1, and all you've shown about argument 1 is `param1`. It's not possible at all to figure out what's wrong with something that isn't shown. – chris Nov 24 '14 at 21:40
  • Apologies I accidentally hit enter which posted it before I was finished writing it. Fixing it now. – user3776749 Nov 24 '14 at 21:43
  • 2
    Still nowhere near the complete error message. There is another line or two containing the specifics, like a conversion error or something. – molbdnilo Nov 24 '14 at 21:47
  • @molbdnilo the error is the entirety of what the compiler spits out at me. The only thing I didn't put is "p5.cpp:29:6: error:" which I will add for completeness. But that is all of it. – user3776749 Nov 24 '14 at 21:51
  • 2
    @user3776749 There will be at least some more lines of output (usually with `Note: ....`). Include them. Or, you know, maybe post a [sscce](http://sscce.org) – sehe Nov 24 '14 at 21:52
  • What type is `Hash`? – Max Lybbert Nov 24 '14 at 21:58
  • It's like I said, that's the whole message, start to finish. Hash is a hash table type I've defined. I think the answer sehe posted might be the ticket. I will report back soon. – user3776749 Nov 24 '14 at 22:06

1 Answers1

9

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> (or std::array<std::string, N>)
sehe
  • 374,641
  • 47
  • 450
  • 633
  • 1
    upon fixing this, the OP will *likely* encounter a "cannot call non-const member `Hash::itemCount()` from const object `t`" message (or close to that, anyway). It may be `const` already, but given the posted code I somewhat suspect it isn't. (and +1) – WhozCraig Nov 24 '14 at 21:56
  • 2
    Ah don't be pessimist :) I use my crystal ball for "good" (well, ... I hope) – sehe Nov 24 '14 at 21:57
  • ... heh, you have your crystal ball (horked copy-ctor), I have mine =P – WhozCraig Nov 24 '14 at 21:58
  • I think this might be it. Will report back soon. Sadly "using namespace std;" is required for this (school) class, and we aren't allowed to use std::vector for some reason. that part is gettin really frustrating because a lot of the answers to questions I post here involve using std::vector. – user3776749 Nov 24 '14 at 22:08
  • @user3776749 There's nothing inherently wrong with `using namespace std` except you are bringing in a lot of names that may conflict with names in your code; as long as you are aware of what is happening, do what is most convenient for your given situation. Same with your use of raw, fixed-size arrays, feel free to use them but understand their limitations and caveats with respect to dynamic containers like `std::vector`. – Jason C Nov 24 '14 at 22:29
  • @sehe Answers containing sentence that start with "don't use" are generally improved by elaborating on *why* and on what the risks of using those things are (and when it may be acceptable to use them). Otherwise you end up with things like the "goto is considered harmful" culture. A good quality of a programmer is to know *why* they aren't using the things they're told not to use. – Jason C Nov 24 '14 at 22:30
  • 1
    @JasonC LUCKILY they are on SO: [Why is `using namespace std` considered a bad practice](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). The OP already knows that vector is probably a good match here (I think the `topA` is likely an output parameter... Should really be a vector return value then). By the way "goto considered harmful" is a prototypical example of a \**wel-argued** piece. I'd be happy to be ranked in that culture (but I'm probably much too lazy) – sehe Nov 24 '14 at 22:39
  • @sehe "Goto considered harmful" was not the intended title of that paper, and regret was later expressed for good reason, well-stated argument aside. As for the "don't use", you are right, the info is easily searchable, but I hypothesize that the typical audience here (not necessarily speaking of the OP) does not do much in the way of searching! – Jason C Nov 24 '14 at 22:44
  • @JasonC I wager that's not really my problem. I can only prod and suggest. Curiosity will have to spark on the other side :) (By spelling things out over and over we just remove the incentive to start using search) – sehe Nov 24 '14 at 22:46
  • @ sehe, that's what I meant (about the frustrating thing), and I do actually do a lot of reading on various structures, libraries, and classes in my own time, even if I don't use them in class. I spent a month this summer digging into COMs. It's way beyond most of the stuff in my course work, and even though I'm sure I don't grasp a lot of the subtleties it was definitely a profitable venture. I greatly appreciate all the help I get on here, and look forward to giving back once I'm more experienced. – user3776749 Nov 24 '14 at 23:30
  • Also the answer was correct. I suppressed copying in my hashtable class for the obvious reasons. Passing the object by references fixed the problem. – user3776749 Nov 24 '14 at 23:32
  • @sehe I've posted a follow-up question since you seemed to have ignored my comment. [Is string\[\] not a type?](https://stackoverflow.com/questions/27117169/is-string-not-a-type/27117272) –  Nov 25 '14 at 01:45
  • 1
    @remyabel I missed that. I should have probably said "string[]" is not - per se - a complete type. Of course, a static object of type `string[]` can exist and then will get a complete type later on. But, `string[]` as in VLA area C99 thing, not in c++ at all (although g++ might accept it as a GNU extension) – sehe Nov 25 '14 at 09:04
  • @sehe You being a high rep user I thought you were being excessively pedantic about some obscure language feature I was unaware of. Of course it was as simple as I thought it was. Thanks. –  Nov 25 '14 at 09:17