标题:[求助] 类模板用法---(当返回值是结构体变量---)
只看楼主
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
帖 子:605
专家分:2489
注 册: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



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-325179-1-1.html




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

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