具有另一个类的数组类型的构造方法

时间:2016-01-27 12:06:29

标签: c++ constructor

这是我必须解决的模型照片:

enter image description here

我有这堂课:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

    <!-- Appenders -->
    <appender name="console" class="org.apache.log4j.ConsoleAppender">
        <param name="Target" value="System.out" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d %-5p| %m %C%n" />
        </layout>
    </appender>

    <logger name="net.sf.log4jdbc">
        <level value="warn" />
    </logger>

    <!-- Root Logger -->
    <root>
        <priority value="debug" />
        <appender-ref ref="console" />
    </root>
</log4j:configuration>

我有这个主要的:

#include<iostream>
#include<fstream>
using namespace std;

class Word
{
protected:
char *value;
char type[20];
int noChars;
static int noWords;

public:
Word(char *value, char *type)
{
    this->noChars = 0;
    this->value = new char[strlen(value) + 1];
    strcpy(this->value, value);
    strcpy(this->type, type);
    Word::noWords++;
}
Word()
{
    this->noChars = NULL;
    this->value = NULL;
    strcpy(this->type,"Nedeterminat");
}
void operator=(Word &x)
{
    this->noChars = x.noChars;
    strcpy(this->type, x.type);
    this->value = new char[strlen(x.value) + 1];
    strcpy(this->value, x.value);
}
Word(const Word& x){
    this->noChars = x.noChars;
    strcpy(this->type, x.type);
    this->value = new char[strlen(x.value) + 1];
    strcpy(this->value, x.value);   
}
char* getValue()
{
    return this->value;
}
void setType(char* x)
{
    if (x == NULL)
    {
        throw new exception("Tip gresit!");

    }
    else
    {
        strcpy(this->type, x);
    }
}
char &operator[](int i)
{
    if (i >= 0 && i <= (strlen(this->value) - 1))
    {
        return this->value[i];
    }
    else
        cout << endl << "Eroare indice: " << i;
}
static int getNoWords()
{
    return Word::noWords;
}
operator int()
{
    return this->noChars;
}
friend ostream& operator<<(ostream&, Word&);
friend istream& operator>>(istream&, Word&);
};
ostream& operator<<(ostream& consola, Word& x)
{
consola << "Value: " << x.getValue() << endl;
consola << "Type: " << x.type << endl;
consola << "NoChars: " << x.noChars << endl;
return consola;
}
istream& operator>>(istream& consola, Word& x){
cout << "Value: "; consola >> x.value;
cout << "Type: "; consola >> x.type;
cout << "NoChars: "; consola >> x.noChars;
return consola;
}
 int Word::noWords = 0;

class Dictionary{
private:
char *language;
int noWords;
bool isOnline;
Word v[100];
public:
Dictionary(char *language, Word w, int noWords, bool isOnline)  
{
    this->language = new char[strlen(language) + 1];
    strcpy(this->language, language);
    for (int i = 0; i < 100; i++)
    {
        this->v[i] = w;
    }
    this->noWords = noWords;
    this->isOnline = isOnline;
}
};
int main()
{
//1
Word w1("exam", "noun");
/*Word w2;*/
Word w3 = w1;
cout << w3;
//2
cout << endl << "Word value: " << w3.getValue();
Word w2("to take", "noun");
w2.setType("verb");
//3
w3 = w2;
cout << endl << w3;
Word *pw = new Word("pointer", "noun");
delete pw;
//4
cin >> w3; cout << w3;
char character = w3[2];
cout << endl << character;
//5
double noChars = (int)w1;
cout << endl << noChars;
cout << endl << Word::getNoWords() << endl;
//6
Dictionary dictionary1("English", NULL, 0, false);
}

我应该如何更改构造函数?我收到一个错误:

Dictionary dictionary1("English", NULL, 0, false);

我应该如何编写默认构造函数?

2 个答案:

答案 0 :(得分:0)

您不能将null指定给类型Word。如果你想默认它,你应该传递像word()

这样的东西
Dictionary dictionary1("English", word(), 0, false);

修改 更好的方法,IMO,适用于你所拥有的main()是这样的:

class Dictionary{
private:
std::string language;
int noWords;
bool isOnline;
std::array<Word,100> v;
public:
     Dictionary(std::string const& language, Word* w, int noWords, bool isOnline): language (language),isOnline(isOnline ),noWords(noWords)
     {
         for (int i = 0; i < 100; i++){
             this->v[i] = (w==NULL)?word():*w;
         }
      }
};

答案 1 :(得分:0)

  1. NULL无法分配到Word。请尝试使用Word(),这将调用默认构造函数Word。考虑将该函数参数更改为const Word& - 允许匿名临时值绑定到const引用。

  2. 我更希望看到std::string作为成员变量language的类型;使用const std::string&作为函数参数。然后,您不必担心后续delete调用,并定义自己的赋值运算符和复制构造函数。 目前,您的班级会泄漏内存。