好吧,我正在制作一个需要另一个自制类的数组的字典类: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