| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1268 人关注过本帖
标题:[求助] 类模板用法---(当返回值是结构体变量---)
只看楼主 加入收藏
lenghaijun
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2009-10-30
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:4 
[求助] 类模板用法---(当返回值是结构体变量---)
初学啊,从没写过类似代码,在vs上面敲了下,出现以下错误:

1>d:\program and design\visual studio\mfc\learningc\learningc\test.h(45): error C2143: syntax error : missing ';' before '*'
1>d:\program and design\visual studio\mfc\learningc\learningc\test.h(45): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\program and design\visual studio\mfc\learningc\learningc\test.h(45): error C2065: 'Type' : undeclared identifier
源程序是

// test.h
#ifndef TEST_H_H
#define TEST_H_H


template <class Type>
class test
{
public:
    struct point
    {
        Type x;
        Type y;
    };
private:
    point *pointType;

public:

    test(Type a, Type b);

    ~test();

    point* pointer();


};


template <class Type>
test<Type>::test(Type a, Type b)
{
    pointType = new point;
    pointType->x = a;
    pointType->y = b;

}

template <class Type>
test<Type>::~test()
{
    delete pointType;
}

template <class Type>
point *test<Type>::pointer()
{
    return pointType;

}
#endif

// main.cpp

#include "test.h"
#include<iostream>
using namespace std;
void main()
{
    test<int> leng(2,3);
    //cout << leng.pointer();

}

// test.cpp 空
搜索更多相关主题的帖子: 变量 返回值 结构体 用法 模板 
2010-11-06 15:51
lenghaijun
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2009-10-30
收藏
得分:0 
怎么没人回了~~~

2010-11-07 18:24
lintaoyn
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:4
帖 子:606
专家分:2499
注 册:2009-4-8
收藏
得分:0 
template <class Type>
test<Type>::point *test<Type>::pointer()
{
    return pointType;
}

迭代的是人,递归的是神。
2010-11-08 07:46
lenghaijun
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2009-10-30
收藏
得分:0 
回复 2楼 lenghaijun
额 ,貌似还是不行啊

ps: 编译环境为 vs2010
2010-11-09 21:31
zhoufeng1988
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:北京
等 级:贵宾
威 望:27
帖 子:1432
专家分:6329
注 册:2009-5-31
收藏
得分:20 
point是一个内部结构,而且这个内部结构是通过模板类型Type来生成的。之前都没试过这么做,做好的是推荐
使用类模板来定义一个point类。这样也会省去很多不必要的麻烦。下面是是修改了你的代码,在VS2008编译通
过:
-------------------------------------------------------------------------------------------------
程序代码:
// test.h
#ifndef TEST_H_H
#define TEST_H_H

template< class Type>
class point
{
public:
    Type x;
    Type y;
};

template <class Type>
class test
{
private:
    point<Type> *pointType;

public:

    test(Type a, Type b);

    ~test();

    point<Type>* pointer();


};

template <class Type>
test<Type>::test(Type a, Type b)
{
    pointType = new point<Type>;
    pointType->x = a;
    pointType->y = b;

}

template <class Type>
test<Type>::~test()
{
    delete pointType;
}

template <class Type>
::point<Type> *test<Type>::pointer()
{
    return pointType;

}
#endif

// main.cpp
#include<iostream>
using namespace std;
void main()
{
    test<int> leng(2,3);
    cout << leng.pointer();
}
// test.cpp 空
你试试这样行不行。
2010-11-10 09:54
快速回复:[求助] 类模板用法---(当返回值是结构体变量---)
数据加载中...
 
   



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

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