發新話題

[問題] [c++] vector如何在class的建構式設定初值?

[c++] vector如何在class的建構式設定初值?

各位大大,下面分別2個檔案t001.cpp 是可以執行t002.cpp & t002_class.h & t002_class.cpp 卻不行執行,但只是把t001.cpp分開寫在不同file,
這是為何呢?

t001.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#include <iostream>
#include <vector>
#include <tchar.h>
#include <cstdlib>
#include <ctime>
using namespace std ;
//---------------------------------------------------------------------------
class bbc
{
        private:
                int news;
                vector<int> title ;
        public:
                bbc(void) ;
                ~bbc(void);
                void output(void) ;
};
//---------------------------------------------------------------------------
bbc::bbc(void) : title(news)
{
        news = 8 ;
        for( int i=0 ; i < news ; ++i )
        {
                title[i] = i ;
        }
}

bbc::~bbc(void)
{
}

void bbc::output(void)
{
        news++;
        cout << news << "\n";
        for( int i=0 ; i < news ; ++i )
        {
                cout << "title[" << i << "] :" << title[i] << "\n" ;
        }
}
//---------------------------------------------------------------------------
void main()
{
        bbc today ;
        today.output() ;

        system("PAUSE");
}

[[i] 本帖最後由 learnmore 於 2014-3-14 16:16 編輯 [/i]]

TOP

// t002.cpp
#include <vcl.h>
#include <iostream>
#include <vector>
#include <tchar.h>
#include <cstdlib>
#include <ctime>
using namespace std ;

#include "t002_class.h"


void main()
{
        bbc today ;
        today.output() ;

        system("AUSE");
}

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

//---------------------------------------------------------------------------
// t002_class.h
#ifndef t002_classH
#define t002_classH
//---------------------------------------------------------------------------
#endif


class bbc {
        private:
                int news;
                vector<int> title ;
        public:
                bbc(void) : title(news) ;

                ~bbc(void);
                void output(void) ;
};
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

// t002_class.cpp
//---------------------------------------------------------------------------
#include <vcl.h>
#include <iostream>
#include <vector>
#include <tchar.h>
#include <cstdlib>
#include <ctime>
using namespace std ;
#include "t002_class.h"
//---------------------------------------------------------------------------
bbc::bbc(void) : title(news) {
        news = 8 ;
        for( int i=0 ; i < news ; ++i ) {
                title = i ;
        }
}
//---------------------------------------------------------------------------
bbc::~bbc(void)
{

}


//---------------------------------------------------------------------------
void bbc:utput(void)
{
        news++;
        cout << news << "\n";
        for( int i=0 ; i < news ; ++i ) {
                cout << "title[" << i << "] :" << title << "\n" ;
    }
}
//---------------------------------------------------------------------------
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

TOP

發新話題

本站所有圖文均屬網友發表,僅代表作者的觀點與本站無關,如有侵權請通知版主會盡快刪除。