English 中文(简体)
创建指针数组和分配时出现问题
原标题:Problem with creating array of pointers and assigning
  • 时间:2011-05-29 10:00:49
  •  标签:
  • c++

问题是

“创建一个指向Reader对象的至少四个指针的数组。使用New运算符可以创建指向派生类对象的至少三个指针,并将它们分配给数组”

The reader is the base class. The fantasyReader, horrorReader, misteryReader, and scienceReader are derived class.

我必须阅读Reader.txt

"""""""""""contents""""""""""""
David
0   <-Mystery category
John
1   <-Horror category
Mark
2   <-Science category
Sarah
3   <-fantasyReader
"""""""""""""""end"""""""""""""""""""

我拥有的

enum {HORROR, MYSTERY, SICENCE, FANTASY}; 

int main(void)
{
    Reader *obj[10];

    ifstream reader_file;

    int category =0; 
    string name; 
    string number; 
    int counter = 0;
    if(reader_file.is_open())
    {

        while( getline(reader_file, name,  
 ) && 
                getline(reader_file, number,  
 ))
        { 
            switch(category)
            {
                case FANTASY:
                    obj[counter++] = new fantasyReader(name);
                    break;
                case MYSTERY: 
                    obj[counter++] = new mysteryReader(name);
                    break
                case HORROR:
                    obj[counter++] = new horrorReader(name);
                    break;
                case SCIENCE:
                    obj[counter++] = new scienceReader(name);
                    break;
            }
        }
    }
}

我不确定我的代码是否回答了上面的问题。

问题回答

您的问题是读取字符串数据(字符),但枚举值对应于整数。尝试:

category = atoi (number.c_str());

switch(category) {
      ...
}

另外,不要忘记打开和关闭文件:

reader_file.open ("readers.txt", ifstream::in);
...
reader_file.close();




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签