好吧,我正在製作一個需要另一個自制類的數組的字典類: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
在此先感謝...
您認爲這樣做有什麼用? '&(「Word」,「Definition」)' – 2011-06-16 16:27:16
'Entry entries [2] = {&(「Word」,「Definition」),&(「Otherword」,「OtherDefiniin」)};'是的,不是。 – 2011-06-16 17:06:45
@Tomalak Geret'kal&放在那裏,因爲Entry()沒有工作和Entry()沒有工作......()沒有工作,所以我認爲把&符號放在那裏會使它成爲一個參考。 。編譯器沒有給我一個直接的錯誤,所以我認爲它是好的....對不起 – luckyl 2011-06-16 17:56:41