| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5255 人关注过本帖
标题:求助!VS2005中 “无法解析的外部命令”问题
取消只看楼主 加入收藏
紫凤双飞
Rank: 2
等 级:论坛游民
帖 子:76
专家分:61
注 册:2011-3-26
结帖率:75%
收藏
已结贴  问题点数:40 回复次数:4 
求助!VS2005中 “无法解析的外部命令”问题
刚刚看了命名空间,就试用了一下,结果出现“无法解析的外部命令” 望解释一下
//stack.h
#ifndef _STACK_H_
#define _STACK_H_

namespace STACK
{
    const int maxsize=50;
    int UNDERFLOW=0;//下溢
    int OVERFLOW=0;//上溢
    template<class T>
    class stack
    {
    private:
        T *data;
        int index;
        int max;//该栈的最大长度
    public:
        stack(int num=maxsize);
        ~stack();
        void push(T);
        T pop();
        T gettop();
        int length();
        bool isempty();
        void print()const;
    };
}

#endif

//stack.cpp
#include<iostream>
#include"stack.h"
using namespace std;

template<class T>
STACK::stack<T>::stack(int num = maxsize)
{
    data=new T(num);
    index=0;
    max=num;
}

template<class T>
STACK::stack<T>::~stack()
{
    delete [] data;
}

template<class T>
T STACK::stack<T>::gettop()
{
    try
    {
        if(index==0)
            throw UNDERFOLW;
        return data[index];
    }
    catch UNDERFLOW
    {
        cout<<"发生下溢"<<endl;
    }
}

template<class T>
void STACK::stack<T>::push(T t)
{
    try
    {
        if(index==max)
            throw OVERFLOW;
        data[index];
        ++index;
    }
    catch OVERFLOW
    {
        cout<<"发生上溢"<<endl;
    }
}

template<class T>
T STACK::stack<T>::pop()
{
    try
    {
        if(index==0)
            throw UNDERFLOW;
        --index;
        return data[index];
    }
    catch UNDERFLOW
    {
        cout<<"发生下溢"<<endl;
    }
}

template<class T>
int STACK::stack<T>::length()
{
    return index;
}

template<class T>
bool STACK::stack<T>::isempty()
{
    if(index==max)
        return true;
    return false;
}

template<class T>
void STACK::stack<T>::print() const
{
    int j=length();
    for(int i=0;i<j;++i)
        cout<<data[i]<<' ';
    cout<<endl;
}
//main.cpp
#include<iostream>
#include"stack.h"
using namespace std;

void main()
{
    STACK::stack<int> intstack(10);
    STACK::stack<char> charstack(8);
    if(intstack.isempty()==true)
        cout<<"Int stack is empty"<<endl;
    else
        cout<<"Int stack isn't empty"<<endl;
    for(int i=0;i<9;++i)
        intstack.push(i);
    intstack.print();
    for(int i=0;i<5;++i)
    {
        char ch;
        ch='a'+i;
        charstack.push(ch);
    }
    charstack.print();
}
结果链接时出错:
1>------ 已启动生成: 项目: stack, 配置: Debug Win32 ------
1>正在编译...
1>main.cpp
1>正在链接...
1>main.obj : error LNK2005: "int STACK::UNDERFLOW" (?UNDERFLOW@STACK@@3HA) 已经在 stack.obj 中定义
1>main.obj : error LNK2005: "int STACK::OVERFLOW" (?OVERFLOW@STACK@@3HA) 已经在 stack.obj 中定义
1>main.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall STACK::stack<int>::~stack<int>(void)" (??1?$stack@H@STACK@@QAE@XZ),该符号在函数 _main 中被引用
1>main.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall STACK::stack<char>::~stack<char>(void)" (??1?$stack@D@STACK@@QAE@XZ),该符号在函数 _main 中被引用
1>main.obj : error LNK2019: 无法解析的外部符号 "public: void __thiscall STACK::stack<char>::print(void)const " (?print@?$stack@D@STACK@@QBEXXZ),该符号在函数 _main 中被引用
1>main.obj : error LNK2019: 无法解析的外部符号 "public: void __thiscall STACK::stack<char>::push(char)" (?push@?$stack@D@STACK@@QAEXD@Z),该符号在函数 _main 中被引用
1>main.obj : error LNK2019: 无法解析的外部符号 "public: void __thiscall STACK::stack<int>::print(void)const " (?print@?$stack@H@STACK@@QBEXXZ),该符号在函数 _main 中被引用
1>main.obj : error LNK2019: 无法解析的外部符号 "public: void __thiscall STACK::stack<int>::push(int)" (?push@?$stack@H@STACK@@QAEXH@Z),该符号在函数 _main 中被引用
1>main.obj : error LNK2019: 无法解析的外部符号 "public: bool __thiscall STACK::stack<int>::isempty(void)" (?isempty@?$stack@H@STACK@@QAE_NXZ),该符号在函数 _main 中被引用
1>main.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall STACK::stack<char>::stack<char>(int)" (??0?$stack@D@STACK@@QAE@H@Z),该符号在函数 _main 中被引用
1>main.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall STACK::stack<int>::stack<int>(int)" (??0?$stack@H@STACK@@QAE@H@Z),该符号在函数 _main 中被引用
1>F:\Users\Administrator\Documents\Visual Studio 2005\Projects\stack\Debug\stack.exe : fatal error LNK1120: 9 个无法解析的外部命令
1>生成日志保存在“file://f:\Users\Administrator\Documents\Visual Studio 2005\Projects\stack\Debug\BuildLog.htm”
1>stack - 12 个错误,0 个警告
========== 生成: 0 已成功, 1 已失败, 0 最新, 0 已跳过 ==========

请问这是什么问题,如何解决啊?
搜索更多相关主题的帖子: index 空间 
2011-05-09 22:33
紫凤双飞
Rank: 2
等 级:论坛游民
帖 子:76
专家分:61
注 册:2011-3-26
收藏
得分:0 
回复 4楼 zhoufeng1988
修改之后,那个无法解析的外部命令没了
但有了重定义问题:
1>------ 已启动生成: 项目: stack, 配置: Debug Win32 ------
1>正在编译...
1>stack.cpp
1>Generating Code...
1>Compiling...
1>main.cpp
1>Generating Code...
1>正在链接...
1>stack.obj : error LNK2005: "int STACK::UNDERFLOW" (?UNDERFLOW@STACK@@3HA) 已经在 main.obj 中定义
1>stack.obj : error LNK2005: "int STACK::OVERFLOW" (?OVERFLOW@STACK@@3HA) 已经在 main.obj 中定义

1>F:\Users\Administrator\Documents\Visual Studio 2005\Projects\stack\Debug\stack.exe : fatal error LNK1169: 找到一个或多个多重定义的符号
1>生成日志保存在“file://f:\Users\Administrator\Documents\Visual Studio 2005\Projects\stack\Debug\BuildLog.htm”
1>stack - 3 个错误,0 个警告
========== 生成: 0 已成功, 1 已失败, 0 最新, 0 已跳过 ==========

在那部分加了#ifndef...也没用:
程序代码:
#ifndef _UNDERFLOW_
#define _UNDERFLOW_
    int UNDERFLOW=0;//下溢
#endif
#ifndef _OVERFLOW_
#define _OVERFLOW_
    int OVERFLOW=0;//上溢
#endif


这个该怎么办啊?
2011-05-10 15:17
紫凤双飞
Rank: 2
等 级:论坛游民
帖 子:76
专家分:61
注 册:2011-3-26
收藏
得分:0 
回复 6楼 tisyang
不是,是可以跨文件的,就像4楼说的那样,而且之前看到一本书上提到的,所以才会尝试嘛
2011-05-10 15:26
紫凤双飞
Rank: 2
等 级:论坛游民
帖 子:76
专家分:61
注 册:2011-3-26
收藏
得分:0 
回复 6楼 tisyang
而且现在是重定义的问题啊
2011-05-10 15:27
紫凤双飞
Rank: 2
等 级:论坛游民
帖 子:76
专家分:61
注 册:2011-3-26
收藏
得分:0 
回复 10楼 tisyang
的却是这样,那要是要用模版又想实现信息影藏,有没有什么办法啊?
2011-05-10 15:53
快速回复:求助!VS2005中 “无法解析的外部命令”问题
数据加载中...
 
   



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

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