标题:新手 遇到的 C++编译错误,望高手指点一二。
只看楼主
xiezhaoxian
Rank: 1
等 级:新手上路
帖 子:5
专家分:8
注 册:2011-3-20
结帖率:0
已结贴  问题点数:20 回复次数:2 
新手 遇到的 C++编译错误,望高手指点一二。
lesson8.h

#ifndef lesson8_h
#define lesson8_h

#include <iostream>
using namespace std;
#define unlong unsigned long
#define unint unsigned int  
class leftover
{
public:
    unlong left (unlong num,unint ct);   
    char *left (const char *str,int n);
};
#endif

lesson8.cpp
#ifndef lesson8_cpp
#define lesson8_cpp

#include "lesson8.h"
unlong leftover::left (unlong num,unint ct)
{
    unint digits =1;
    unlong n =num;
    if(ct==0 || num==0)
    {
        return 0;
    }
    while(n/=10)
    {
        digits++;
    }
    if(digits>ct)
    {
        ct=digits-ct;
        while(ct--)
        {
            num /=10;
        }
        return num;
    }
    else
    {
        return num;
    }
}

char leftover::*left (const char *str,int n)
{
    if(n<0)
    {
        n=0;
    }
    char *p=new char[n+1];
    int i;
    for (i=0;i<n&&str[i];i++)
    {
        p[i]=str[i];
    }
    while(i<=n)
    {
        p[i++]='\0';
    }
    return p;
}
#endif

my_lesson8.cpp

#ifndef my_lesson8_cpp
#define my_lesson8_cpp

#include "lesson8.h"
leftover MoveLeft;
int main()
{
    char *trip =" hawaii!! ";
    unlong n = 12345678;
    int i;
    char *temp;
    for(i=1;i<10;i++)
    {
        cout<<MoveLeft.left(n,i)<<endl;
        temp=MoveLeft.left(trip,i);
        cout<<temp<<endl;
        delete []temp;
    }
    return 0;
}
#endif

错误信息
--------------------Configuration: lesson8 - Win32 Debug--------------------
Compiling...
lesson8.cpp
e:\c#\c++ project\lesson8\lesson8.cpp(65) : error C2440: 'return' : cannot convert from 'char *' to 'char leftover::*'
        There is no context in which this conversion is possible
Error executing cl.exe.

lesson8.exe - 1 error(s), 0 warning(s)
搜索更多相关主题的帖子: include public return 
2011-11-13 12:09
Toomj
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
帖 子:257
专家分:1826
注 册:2011-5-17
得分:10 
char leftover::*left (const char *str,int n)
改为:char* leftover::left (const char *str,int n)
2011-11-13 13:49
dhyco
Rank: 2
等 级:论坛游民
帖 子:9
专家分:32
注 册:2011-3-29
得分:10 
2楼正解!!
2011-11-13 14:28



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




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

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