2011-06-16 93 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

相關問題