所以我有一个类“Sentence”#包含“单词”。没有定义类函数的返回类型
句子是单词的链接列表。
我的任务规定,我不得不超负荷“运营商+”,使
Sentence s = "dog jumped.";
Word w = "The";
W+s; //should return a new sentence that says "The dog jumped high."
请记住,我必须重载operator +。这就是我正在分级
但是,因为,句子包括单词,它尚未定义。我会得到一个错误
return type 'class Sentence' is incomplete
和 无效使用不完全类型“常量类句子”的
这里是我的过载码
class Sentence; //forward declaration
Sentence Word::operator+(const Sentence &sentence) const{
Sentence *s = new Sentence(sentence.getCopy()); //make a new sentence that's a copy of the parameter
Word *w = new Word;
Sentence::node *l = new Sentence::node; //make new linked list node
(*(l->w)) = (*w); //Set word of node
l->next = (*s).getFirs(); // set new node to point to first node of the sentence object
(*s).setFirs(l); // point first pointer to the new node
return *s;
}
我也尝试过载的一个单独的方法看起来很喜欢的班级以外的运营商
Sentence operator+(const Word &word, const Sentence &sentence);
其中导致错误,说它被定义多次
你试图返回时只能向前声明的类型。假设你是编译器,用户说将来会有Sentance类,所以你只知道那一件事,然后他试图返回它的实例。你真的不知道大小是多少,所以临时分配多少空间等等。您将不得不拆分此代码,以便操作员+位于可以看到句子和词的定义的位置。 – Creris 2014-10-19 18:11:18
在此类以外定义此操作员是可行的方法,可能需要朋友声明或实用程序功能。为什么它不起作用?我不知道,你一定犯了一个错误,但是不看代码就说不出来(当然这只是一个简单的例子)。 – 2014-10-19 18:14:52
您似乎已经发布了几天的相同代码,但仍未解决将“Word”和“Sentence”的类定义放在哪里的基本问题。首先解决这个问题,然后所有其他问题都会消失。 – 2014-10-19 21:36:23