English 中文(简体)
固定成员错误
原标题:String static member error

我正在制定一项基本方案,其中吸收了一些投入,并重新印制,但我正在对我静态变量产生奇怪的错误。 请帮助我。 增 编

法典:

/*

    Date:5th January 2011
    Programmer:Fahad
*/
#include <iostream>
#include <string>
using namespace std;
class Persons //A class that will store the name,addresses and id numbers of the users
{
    private:
        string name_;
        string address_;
        int id_number_;

    public:
        static int count;//This is the count of the objects created
        Persons();
        void getData(int n);
        void displayData(int n);
        //~Persons();
};
static int count;//initializing the static member
int main()
{
    cout << "Enter Number Of Persons:";
    int n;//This is the number of objects that the user wants to make.
    cin >> n;
    Persons *ptr;//A pointer that will be used for the dynamic memory allocation.

    /*Exception Handling*/
    ////////////////////////////////////////////////////////////////////
    try
    {
        //ptr=new [sizeof(Persons) * n];
        ptr=new Persons[n];
    }
    catch(bad_alloc xa)
    {
        cout<<"Sorry,Program Can Not Continue";
        cin.get();
        exit(1);
    }
    /////////////////////////////////////////////////////////////////////
    for(int i = 0; i< n; i++)
    {
        ptr[i].getData(n);
    }
    for(int j = 0; j< n; j++)
    {
        ptr[j].displayData( n );
    }
    cin.get();
    delete[] ptr;
    return 0;
}
/*Function Definitions*/
Persons::Persons()
{
    name_="";
    address_="";
    id_number_=0;
    count++;
}
void Persons::getData(int n)
{

        cout<<"Enter Name (Press  $  To Exit):";
        getline(cin,name_, $ );
        cout<<endl<<"Enter Address (Press  $  To Exit):";
        getline(cin,address_, $ );
        cout<<endl<<"Enter Identitiy Card Number:";
        cin>>id_number_;

}
void Persons::displayData(int n)
{

        cout<<"Name:"<<name_;
        cout<<endl<<"Address:"<<address_;
        cout<<endl<<"Identitiy Card Number:"<<id_number_;

}

错误:

------ Build started: Project: new, Configuration: Debug Win32 ------
Compiling...
program.cpp
c:documents and settings1my documentsvisual studio 2008projects
ew
ewprogram.cpp(23) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Build log was saved at "file://c:Documents and Settings1My DocumentsVisual Studio 2008Projects
ew
ewDebugBuildLog.htm"
new - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
?
最佳回答

而不是

static int count;//initializing the static member

write

int Persons::count;
问题回答

你的方案有两个问题:

Problem no 1) Missing declaration for exit(1);.
Solution : Include <cstdlib>

Problem no 2) static member count declared but not defined.
Solution : Define the member as int Persons::count; instead of static int count.

您需要将静态变量定义为int Persons:count; 另外,getData &displayData syntax也是错误的。 您需要使用<代码>->营运人,而不是<代码>。





相关问题
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?

热门标签