2011-06-16 98 views
0

好吧,我正在制作一个需要另一个自制类的数组的字典类:Entry ...我试图让项的数组...我最终设法摆脱了大部分的错误......,除了一个,指出错误是在cstdio:错误:从'const char(*)[11]'转换为非标量类型'L :: Entry'请求g ++ 4.4.5

error: conversion from ‘const char (*)[11]’ to non-scalar type ‘L::Entry’ requested

我不能找出什么毛病,但我已经查明该数组初始化是错误启动...继承人,测试我的Entry类为我的主文件的代码:

#include "Entry.cpp" 
#include <iostream> 
#include <cstdlib> 
#include <cstdio> 
using namespace std; 
using namespace L; 
int main(){ 
Entry entry("word", "definition"); 
cout << "entry's word is: " << entry.getWord() << endl; 
cout << "entry's definition is: " << entry.getDefinition() << endl; 

cout << "\n\n\n" << endl; 

Entry entries[2] = { 
    &Entry("Word", "Definition"), 
    &Entry("Otherword", "OtherDefiniin") 
}; 

cout << "entries's word is: " << entries[1].getWord() << endl; 
cout << "entries's definition is: " << entries[1].getDefinition() << endl; 
return 0; 
} 

这里是Entry.cpp:

#include "Entry.h" 
#include <string.h> 
namespace L 
{ 
//constructors and destructors 
Entry::Entry(const char *word, const char *def) : word(word), def(def){} 
Entry::Entry(Entry &entryObj) : word(entryObj.word), def(entryObj.def){} 
Entry::~Entry(){} 
//setter methods 
void Entry::setWord(char *newWord){Entry::word = newWord;} 
void Entry::setDefinition(char *newDef){Entry::word = newDef;} 
//getter methods 
std::string Entry::getWord(){return Entry::word;} 
std::string Entry::getDefinition(){return Entry::def;} 
} 

终于Entry.h:

#include <cstdlib> 
#include <cstring> 
#include <string> 
#include <string.h> 
#ifndef ENTRY_H 
#define ENTRY_H 
namespace L 
{ 
    class Entry 
     { 
     public: 
      //constructors and destructors 
      Entry(const char *word = "", const char *def = ""); 
      Entry(Entry &entryObj); 
      virtual ~Entry(); 
      //setter methods 
      void setWord(char *newWord); 
      void setDefinition(char *newDef); 
      //getter methods 
      std::string getWord(); 
      std::string getDefinition(); 
     private: 
      std::string word; 
      std::string def; 

     }; 


} 
#endif 

在此先感谢...

+2

您认为这样做有什么用? '&(“Word”,“Definition”)' – 2011-06-16 16:27:16

+0

'Entry entries [2] = {&(“Word”,“Definition”),&(“Otherword”,“OtherDefiniin”)};'是的,不是。 – 2011-06-16 17:06:45

+1

@Tomalak Geret'kal&放在那里,因为Entry()没有工作和Entry()没有工作......()没有工作,所以我认为把&符号放在那里会使它成为一个参考。 。编译器没有给我一个直接的错误,所以我认为它是好的....对不起 – luckyl 2011-06-16 17:56:41

回答

5
Entry entries[2] = { 
    &("Word", "Definition"), 
    &("Otherword", "OtherDefiniin") 
}; 

什么是&(....)

我认为你的意思,

Entry entries[2] = { 
    Entry("Word", "Definition"), 
    Entry ("Otherword", "OtherDefiniin") 
}; 

此外,

Entry::Entry(const char *word, const char *def){ 
    strcpy(Entry::word, word); 
    strcpy(Entry::def, def); 
} 

首先,你为什么写Entry::word?此外,您还没有将内存分配给word

我会建议你使用std::string,而不是char*为:

//put them in the class definition 
std::string word; 
std::string def; 

//constructor definition outside the class 
Entry::Entry(const char *word, const char *def) : word(word), def(def) { } 

,并删除其他的构造函数需要非const char*。它不需要!

+1

+1良好的工作伙伴 – 2011-06-16 17:07:15

+0

好的......我越来越接近......在放入代码后,只有一个错误,当它与Entry(“Word”,“Definition” )它告诉我它可以找到一个具有参数的构造函数:L :: Entry ...当我进入头文件和cpp文件并摆脱&sign并重新编译时,可能的候选项是L :: Entry&...它给了我一个关于这个错误 – luckyl 2011-06-16 17:10:35

+0

好吧...我甚至更远...我添加到条目(“Word”,“定义”)的前面,但它给了我和错误说.. ..从'L :: Entry *'为非标量类型'L :: Entry'请求 – luckyl 2011-06-16 17:15:06

0

我想通了......在与纳瓦兹给我的代码混淆之后。我终于,由于某种原因,使所有的警告,结果发现“的临时地址”我看着它,然后决定尝试使阵列等于预先初始化的对象...即

Entry *entry("Word","Definition"); 
Entry *entry2("Word2","Definition2"); 
Entry *entries[2]; 
entries[0] = &entry; 
entries[1] = &entry2; 

它最终没有任何工作运行时错误或编译器错误

+0

我现在已经学会了永远启用-Wall并始终观看警告 – luckyl 2011-06-17 16:41:32

相关问题