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 []'