0

I'm having an issue writing my first function call from a class. First, I'm having trouble using a string array as a parameter in my Unio and Intersect function definitions in the header file. I know arrays have to be passed to functions by reference but I don't see how I'm causing a problem. I've seen it done in examples this way.

Second, the sets I'm creating in the Unio and Intersect functions won't accept the arrays I'm passing as arguments. I imagine it has something to do with not being able to declare anything in the header file but I don't see an alternative to the way I'm passing the arrays into the sets.

Third, I'm trying to call two functions: Unio() and Intersect() in int main() but it's saying they are both undefined. I know I need Class::Function or Object.Function, but the both the Unio and Intersect functions are not attached to objects and when I attach them to their class, Union::Unio or Intersection::Intersect, I'm told "A nonstatic member reference must be relative to a specific object."

#include <string>
#include <cstring>
#include <set>
#include <iostream>
#include <iterator>


#ifndef UNIONSET
#define UNIONSET

class Union
{
    public:
        void Unio(int capture, int capture2, string str1[], string str2[]);

    private:
        int g, i, h;
        string last[100];
        set<string> newset1(last, last+100);
        set<string>::iterator it;

};

#endif

#ifndef INTERSET
#define INTERSET

class Intersection
{
public:
    void Intersect(string str3[], string str4[]);

private:
    set<string> newset2(str3, str3+100);
    set<string> newset3(str4, str4+100);
    set<string>::iterator it2;
};

#endif
<code>

/*
    Andrew_Spiteri_hw3 is an attempt at the third homework assignment for CSE 232.
    This program is designed to test two sets for union and intersection between them and
    subsquently display the results.
*/

#include <string>
#include <iostream>
#include <set>
#include <cstring>
#include <iterator>
#include "Set.h"
using namespace std;

void Union::Unio(int capture2, int capture, string str1[], string str2[])
{
    int g = 0;

    string last[100];
    for(int i = 0; i < capture; ++i)
    {
        for(int h = 0; h < capture2; ++h)
        {
            if(str1[i] == str2[h])
            {
                last[g] = str1[i];
                ++g;
            }
        }       
    }
    set<string> newset1(last, last+100);
    set<string>::iterator it;
    cout<<"The numbers constitute the intersection between the sets.\n";
    for(it=newset1.begin(); it!=newset1.end(); ++it)
    {
        cout << *it<<' ';
    }   
    cout<<'\n';
};

void Intersection::Intersect(string str3[], string str4[])
{
    set<string> newset2(str3, str3+100);
    set<string> newset3(str4, str4+100);
    newset2.insert(newset3.begin(), newset3.end());
    set<string>::iterator it2;
    cout<<"This set constitutes a union between the two sets."<<'\n';
    for(it2=newset2.begin(); it2!=newset2.end(); ++it2)
    {
        cout << *it2<<' ';
    }   
    cout<<'\n';
};

int main()
{
    string set1, set2;
    string extra1[100], extra2[100];
    cout<<"Enter your first set."<<'\n';
    getline(cin,set1);
    char *ch1 = new char[100];
    strcpy(ch1, set1.c_str());
    cout<<"Enter you second set."<<'\n';
    getline(cin,set2);
    char *ch2 = new char[100];
    strcpy(ch2, set2.c_str());
    int z = 0;


    for(int i = 0; i < set1.size(); ++i)
    {
        const char c = ch1[i];
        if(c == ' ')
        {
            ++z;
            continue;
        }
        else if (c == ',')
        {
            continue;
        }
        else if (c >= '0' && c <= '9')
        {
            extra1[z] += ch1[i];    

        }
        else
            continue;
    }   

    int capture = z + 1;
    int r = 0;

    for(int i = 0; i < set2.size(); ++i)
    {
        const char c = ch2[i];
        if(c == ' ')
        {
            ++r;
            continue;
        }
        else if (c == ',')
        {
            continue;
        }
        else if (c >= '0' && c <= '9')
        {
            extra2[r] += ch2[i];    

        }
        else
            continue;
    }   

    int capture2 = r + 1;
    Unio(capture, capture2, &extra1, &extra2);
    Intersect(&extra1, &extra2);
}

Here are the errors I'm getting if anyone is interested.

After adding using namespace std; to the header file I'm getting a lot fewer error messages.

This is what's left.

c:\users\andrew\documents\visual studio 2012\projects\andrew_spiteri_hw3\andrew_spiteri_hw3\set.h(21): error C2061: syntax error : identifier 'last'
c:\users\andrew\documents\visual studio 2012\projects\andrew_spiteri_hw3\andrew_spiteri_hw3\set.h(37): error C2061: syntax error : identifier 'str3'
c:\users\andrew\documents\visual studio 2012\projects\andrew_spiteri_hw3\andrew_spiteri_hw3\set.h(38): error C2061: syntax error : identifier 'str4'
c:\users\andrew\documents\visual studio 2012\projects\andrew_spiteri_hw3\andrew_spiteri_hw3\intset.cpp(74): warning C4018: '<' : signed/unsigned mismatch
c:\users\andrew\documents\visual studio 2012\projects\andrew_spiteri_hw3\andrew_spiteri_hw3\intset.cpp(98): warning C4018: '<' : signed/unsigned mismatch
c:\users\andrew\documents\visual studio 2012\projects\andrew_spiteri_hw3\andrew_spiteri_hw3\intset.cpp(120): error C2664: 'Union::Unio' : cannot convert parameter 3 from 'std::string (*)[100]' to 'std::string []'
Spitz
  • 63
  • 1
  • 9
  • You shouldn't mix c strings (char*) with c++ strings. –  Feb 18 '14 at 04:01
  • Yeah, that's what I see everywhere but it's how our professor is giving us examples. – Spitz Feb 18 '14 at 04:06
  • Also you should use a `vector` instead of an array of strings. –  Feb 18 '14 at 04:07
  • When I take void off I get an error that states, "Explicit type is missing('int' assumed). I'm creating a function within the class in this instance correct, not a constructor? Wouldn't the function need a return type? – Spitz Feb 18 '14 at 04:08
  • Oops, I got confused by your naming, the methods aren't really constructors, their name is just similar to the class name. –  Feb 18 '14 at 04:10
  • I'll start looking into that. Thanks for the advice. – Spitz Feb 18 '14 at 04:10
  • SO, void would be appropriate in this instance right? – Spitz Feb 18 '14 at 04:11
  • Yes, but I don't see the purpose of the classes, all you do is trying to call their methods (which by the way is done wrongly, you need to call for example `Unio` on an instance of `Union` or declare it as `static` and then call with `Union::Unio(...)`.) Why don't you just use simple functions? –  Feb 18 '14 at 04:12
  • The first three error message left are because `using namespace std;` is in the wrong place, put it one line before `#include "Set.h"`, although as mentioned below in general it is dangerous to use that in this way. –  Feb 18 '14 at 04:17
  • Yeah, I declared the function void and it looks much cleaner. – Spitz Feb 18 '14 at 04:21

3 Answers3

2

For standard names declared in namespace std you have to specify prefix std:: before them.

So instead of string you have to write std::string, before map you have to write std::map

Or you can include directive in your header where you defined classes

using namespace std;

instead of specifying qualified names.

Or you can introduce each standard name in the global namespace. For example

using std::string;

and so on.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Do **not** put `using namespace std;` in a header file. the rest of this I'm on board with. – WhozCraig Feb 18 '14 at 03:54
  • Are you referring to the header file? Putting `using namespace std;` in the header took away a lot of error messages. What's wrong with using it? – Spitz Feb 18 '14 at 03:56
  • @WhozCraig it is a useless advice because he only starts to learn C++ and it seems uses examples or exercises from some book for beginners. – Vlad from Moscow Feb 18 '14 at 03:57
  • @user3319779 Yes, I mean the header file. – Vlad from Moscow Feb 18 '14 at 03:58
  • @user3319779 Since you asked [Read this and the plethora of answers](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). And advice is only useless when it *has no use*, which this does. It is, however, admittedly *futile* to expect a newcomer to C++ is going to care. – WhozCraig Feb 18 '14 at 08:10
  • @WhozCraig Haha, yeah I just got finished reading that. Contrary to your opinion I do care though. I'm not writing anything that has enough code I wouldn't be able to keep track of the variables I'm naming and VS 2012 seems to make sure you aren't using already allocated names pretty well. One of my textbooks uses that syntax already so it shouldn't be hard to get used to it now. – Spitz Feb 18 '14 at 15:39
  • @user3319779 Apologies if that was offensive; it was meant in response to Vlad's assertion the advice was useless. It isn't. It is admittedly futile if *you* (the questioner) don't care. But it sounds like you do, so thats good!. – WhozCraig Feb 18 '14 at 15:45
  • @WhosCraig Naw, it's no prob either way. – Spitz Feb 18 '14 at 15:49
0

you can initialize obj int class definition, it should be in constructor.

set newset1(last, last+100);

and variable scope of str3,str4

set newset2(str3, str3+100); set newset3(str4, str4+100);

michaeltang
  • 2,850
  • 15
  • 18
  • Hey Mike, I'm pretty sure what you're telling me is important. 3 of the 4 error messages I have left to deal with involve last, str3, and str4. I don't understand what you're saying though. Is it that I can't initialize an object in the class definition. I think I said something about that in my post but I don't know how to get around that. – Spitz Feb 18 '14 at 04:15
0

I see the following problems with your code:

  1. I don't know what's the purpose of #include "Set.h". Your code does not seem to be using anything from it.
  2. You need to move using namespace std; up, before the line #ifndef UNIONSET.
  3. Make Union::Unio a static member function.
  4. Remove or comment out the line set newset1(last, last+100); from Union. It seems like you don't need any of the member data of the class. However, the other lines don't create compiler errors.
  5. Make Intersection::Intersect a static member function.
  6. Remove or comment out the lines set newset2(str3, str3+100); and set newset3(str4, str4+100); from Intersection.
  7. Change the call to Union::Unio. It needs to be:

    Union::Unio(capture, capture2, extra1, extra2);

  8. Change the call to Intersection::Intersect. It needs to be:

    Intersection::Intersect(extra1, extra2);

With the above changes, I was able to compile the file. I didn't check whether the logic in Union::Unio and Intersection::Intersect is correct.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • Thank you very much for your help. That was very thorough. It gave me exactly what I needed. – Spitz Feb 18 '14 at 05:26