| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 696 人关注过本帖
标题:实现一个简单的Vector时遇见了一些问题
只看楼主 加入收藏
thlgood
Rank: 5Rank: 5
等 级:职业侠客
帖 子:281
专家分:381
注 册:2010-9-24
结帖率:91.43%
收藏
已结贴  问题点数:20 回复次数:3 
实现一个简单的Vector时遇见了一些问题
我不是编程新手,但也算是C++新手了,今天尝试着写一个简单的Vector,一心想能够实现简单的迭代功能就够了。
可是C++的语法似乎是一道坎啊!

程序代码:
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

template<typename Type>
struct print
{
    void operator()(Type& i)
    {
        cout << i << endl;
    }
};

template<typename T>
class MyVector
{
public:
    typedef T* iterator;
    MyVector(int& size):_size(size):_first(NULL)
    {
        alloc();
    }
    ~MyVector()
    {
        if(NULL == _first)
            return;
        delete _first;
        _first = NULL;
    }
    void alloc() throw(std::bad_alloc)
    {
        try{
            _first = operator new(_size * sizeof(T));
        }catch(...){
            throw;
        }
    }

    iterator begin() 
    {
        return _first;
    }
    iterator end()
    {
        return _first + _size;
    }
private:
//    MyVector();
//    MyVector(MyVector&);
    int _size;
    iterator _first;
};

int main()
{
    int myints[] = {1, 2, 3, 4, 5, 6, 7};
    MyVector<int>myvector(7);

    std::copy(myints, myints + 7, myvector.begin());
    for_each(myvector.begin(), myvector.end(), print<int>());
    return 0;
}


编译出错,编译器的错误提示为:
程序代码:
copy.cpp: 在构造函数‘MyVector<T>::MyVector(int&)’中:
copy.cpp:20:36: 错误: expected ‘{’ before ‘:’ token
copy.cpp: 在函数‘int main()’中:
copy.cpp:57:28: 错误: 对‘MyVector<int>::MyVector(int)’的调用没有匹配的函数
copy.cpp:57:28: 附注: 备选是:
copy.cpp:20:5: 附注: MyVector<T>::MyVector(int&) [with T = int]
copy.cpp:20:5: 附注:   no known conversion for argument 1 from ‘int’ to ‘int&’
copy.cpp:16:7: 附注: MyVector<int>::MyVector(const MyVector<int>&)
copy.cpp:16:7: 附注:   no known conversion for argument 1 from ‘int’ to ‘const MyVector<int>&’
搜索更多相关主题的帖子: 遇见 Vector 
2013-05-17 15:02
thlgood
Rank: 5Rank: 5
等 级:职业侠客
帖 子:281
专家分:381
注 册:2010-9-24
收藏
得分:0 
以上代码中要是有什么不好的编程习惯,也希望各位能够指出,谢谢!

代码中构造函数有什么问题?我找不出来

o(∩∩)Linux & Python 群:187367181
2013-05-17 15:04
hahayezhe
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:湖南张家界
等 级:贵宾
威 望:24
帖 子:1386
专家分:6999
注 册:2010-3-8
收藏
得分:20 
new(_size * sizeof(T));???
不应该是new [size * sizeof(T)]

int& size一个int而已 常量能引用吗?有构造给你??
2013-05-17 15:07
thlgood
Rank: 5Rank: 5
等 级:职业侠客
帖 子:281
专家分:381
注 册:2010-9-24
收藏
得分:0 
回复 3楼 hahayezhe
问题已经解决了:

程序代码:
#include <vector>
#include <iostream>
#include <algorithm>
#include <cstdlib>
using namespace std;

template<typename Type>
struct print
{
    void operator()(Type& i)
    {
        cout << i << endl;
    }
};

template<typename T>
class MyVector
{
public:
    typedef T* iterator;
    MyVector(const int& size):_size(size),_first(NULL)  //这里
    {
        alloc();
    }
    void alloc()
    {
        try{
            _first = new T[_size];                      //这里
        }catch(...){
            throw;
        }
    }
    ~MyVector()
    {
        if(NULL == _first)
            return;
        delete[] _first;
        _first = NULL;
    }

    iterator begin() 
    {
        return _first;
    }
    iterator end()
    {
        return _first + _size;
    }
private:
    MyVector();
    MyVector(const MyVector&);
    int _size;
    iterator _first;
};

int main()
{
    int myints[] = {1, 2, 3, 4, 5, 6, 7};
    MyVector<int>myvector(7);

    std::copy(myints, myints + 7, myvector.begin());
    for_each(myvector.begin(), myvector.end(), print<int>());
    return 0;
}


[ 本帖最后由 thlgood 于 2013-5-17 15:35 编辑 ]

o(∩∩)Linux & Python 群:187367181
2013-05-17 15:23
快速回复:实现一个简单的Vector时遇见了一些问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016888 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved