定义一个类并调用其成员函数

时间:2014-02-18 03:46:46

标签: c++ arrays

我在从类中编写第一个函数调用时遇到问题。首先,我在使用字符串数组作为头文件中UnioIntersect函数定义中的参数时遇到了问题。我知道数组必须通过引用传递给函数,但我不知道我是如何导致问题的。我已经通过这种方式看到了它。

其次,我在UnioIntersect函数中创建的集合将不接受我作为参数传递的数组。我想它与无法在头文件中声明任何内容有关,但我没有看到我将数组传递给集合的方式的替代方法。

第三,我试图在Unio()中调用两个函数:Intersect()int main(),但它说它们都是未定义的。我知道我需要Class :: Function或Object.Function,但是Unio和Intersect函数都没有附加到对象上,当我将它们附加到它们的类Union::UnioIntersection::Intersect时,我就是告诉“非静态成员引用必须与特定对象相关。”

#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);
}

如果有人有兴趣,我会收到以下错误。

using namespace std;添加到头文件后,我收到的错误消息会少得多。

这就是剩下的。

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

3 个答案:

答案 0 :(得分:2)

对于在命名空间std中声明的标准名称,您必须在它们之前指定前缀std::

因此,您必须在string std::string之前写map而不是std::map而不是using namespace std; 。{/ 1>

或者你可以在你定义类

的标题中包含指令
using std::string;

而不是指定限定名称。

或者您可以在全局命名空间中引入每个标准名称。例如

{{1}}

等等。

答案 1 :(得分:0)

你可以初始化obj int类定义,它应该在构造函数中。

设置newset1(最后,最后+ 100);

和str3的变量范围,str4

设置newset2(str3,str3 + 100); 设置newset3(str4,str4 + 100);

答案 2 :(得分:0)

我发现您的代码存在以下问题:

  1. 我不知道#include "Set.h"的目的是什么。您的代码似乎没有使用任何内容。
  2. 您需要在using namespace std;行之前向上移动#ifndef UNIONSET
  3. 使Union::Unio成为静态成员函数。
  4. set newset1(last, last+100);删除或注释掉Union行。您似乎不需要该类的任何成员数据。但是,其他行不会产生编译器错误。
  5. 使Intersection::Intersect成为静态成员函数。
  6. set newset2(str3, str3+100);删除或注释掉set newset3(str4, str4+100);Intersection行。
  7. 将通话更改为Union::Unio。它必须是:

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

  8. 将通话更改为Intersection::Intersect。它必须是:

    Intersection::Intersect(extra1, extra2);

  9. 通过上述更改,我能够编译该文件。我没有检查Union::UnioIntersection::Intersect中的逻辑是否正确。