如何解决声明错误

时间:2019-05-08 12:26:38

标签: c++ scope compilation declaration stdstring

我正在尝试运行一个代码,该代码需要用户输入标准时间并将其转换为军事时间。对于用户输入,小时可以输入一位或两位数字,并且AM / PM可以任何方式输入。鉴于此,我拥有的代码如下:

#include <iostream>
#include <string>

using namespace std;

string time, hour, minute, amPm, miltime;

char amPmCheck;

int main()
{

    cout<<"Enter time:\n";

    cin>>time;

    colon = time.find(':');

    space = time.find (' ');

    hour = time.substr(0, colon);

    minute = time.substr(colon + 1, space);

    amPm = time.substr(space, back);

    amPmCheck = amPm[0];

    timeConversion(hour, minute, amPmCheck);

    return 0;
}


void timeConversion(hour, minute, amPmCheck)
{
    if(amPmCheck == 'a'||'A')
    {
        if(int(hour) == 12)
        {
            hour.assign('00');
        }
        else if (int(hour) <= 9)
        {
            hour.insert(0, '0');
        }
    }
    else
        if(int(hour) < 12)
        {
            hour.assign(12+int(hour));
        }

    miltime = hour + minute

    cout<< "Corresponding military time is", miltime;
}

但是,当尝试编译代码时,我从选择的编译器中收到以下错误:

main.cpp:50:25: warning: multi-character character constant [-Wmultichar]
             hour.assign('00');
                         ^~~~
main.cpp:15:8: error: 'std::string time' redeclared as different kind of symbol
 string time, hour, minute, amPm miltime;
        ^~~~
In file included from /usr/include/pthread.h:24:0,
                 from /usr/include/x86_64-linux-gnu/c++/6/bits/gthr-default.h:35,
                 from /usr/include/x86_64-linux-gnu/c++/6/bits/gthr.h:148,
                 from /usr/include/c++/6/ext/atomicity.h:35,
                 from /usr/include/c++/6/bits/ios_base.h:39,
                 from /usr/include/c++/6/ios:42,
                 from /usr/include/c++/6/ostream:38,
                 from /usr/include/c++/6/iostream:39,
                 from main.cpp:10:
/usr/include/time.h:192:15: note: previous declaration 'time_t time(time_t*)'
 extern time_t time (time_t *__timer) __THROW;
               ^~~~
main.cpp:44:21: error: variable or field 'timeConversion' declared void
 void timeConversion(hour, minute, amPmCheck)
                     ^~~~
main.cpp:44:35: error: 'amPmCheck' was not declared in this scope
 void timeConversion(hour, minute, amPmCheck)
                                   ^~~~~~~~~

我对编程还是比较陌生的,因此,任何有关我的错误以及最简单的解决方法的信息都将受到赞赏。

2 个答案:

答案 0 :(得分:0)

首先是这里的几个问题:

  • 更改

void timeConversion(hour, minute, amPmCheck)

void timeConversion(string & hour, string & minute, string & amPmCheck)

如果要处理函数参数

  • 或将相同的功能(以您的情况为佳)更改为:

void timeConversion()

因为您还是要处理全局变量。

  • timeConversion函数放在main之前,或使用类似这样的预声明:

void timeConversion();

也在main函数之前。

PS。请阅读有关C ++的好书。

答案 1 :(得分:0)

对于初学者而言,无需将这些变量声明为名称空间变量。

string time, hour, minute, amPm, miltime;

char amPmCheck;

如果使用函数main,则应将它们声明为局部变量。

并且没有变量colonspace的声明。

此声明

amPm = time.substr(space, back);

没有道理。变量amPm声明为类型char而不是类型std::string

函数声明

void timeConversion(hour, minute, amPmCheck)

是错误的。您未指定函数参数的类型。

这样的陈述

    if(int(hour) == 12)

没有道理。您必须至少使用标准函数std::stoi才能将类型为std::string的对象转换为类型为int的对象。

在声明中

hour.assign('00');

使用了多字节char文字。看来您的意思是双引号

hour.assign("00");

同样适用于该语句

hour.insert(0, '0');

此声明

hour.assign(12+int(hour));

也没有道理。

您的代码中有太多错误。

首先,您应该阅读std::basic_string类的文档,以了解如何使用其网络。

不要使用指示性

using namespace std;

这可能是导致声明冲突的原因。 例如,查看编译器的错误消息

main.cpp:15:8: error: 'std::string time' redeclared as different kind of symbol
 string time, hour, minute, amPm miltime;
        ^~~~
In file included from /usr/include/pthread.h:24:0,
                 from /usr/include/x86_64-linux-gnu/c++/6/bits/gthr-default.h:35,
                 from /usr/include/x86_64-linux-gnu/c++/6/bits/gthr.h:148,
                 from /usr/include/c++/6/ext/atomicity.h:35,
                 from /usr/include/c++/6/bits/ios_base.h:39,
                 from /usr/include/c++/6/ios:42,
                 from /usr/include/c++/6/ostream:38,
                 from /usr/include/c++/6/iostream:39,
                 from main.cpp:10:
/usr/include/time.h:192:15: note: previous declaration 'time_t time(time_t*)'
 extern time_t time (time_t *__timer) __THROW;
               ^~~~

在名称空间std中声明的标准名称使用限定名称代替。