繁体   English   中英

警告 C4018:“<”:有符号/无符号不匹配仅当我包含相同的函数时

[英]warning C4018: '<': signed/unsigned mismatch ONLY when I include Identical Functions

我迷路了,当我昨晚运行我的程序时,它运行良好。 当我添加power()函数时,突然没有添加新代码就可以正常运行的行现在会触发错误消息:

警告 C4018:“<”:有符号/无符号不匹配

为什么?

我觉得我没有能力解释这一点,所以请按照下面的代码进行操作。

请在有和没有这个power()函数的情况下运行代码 当使用power()函数运行时,它会在exam()函数的for循环中产生错误 C4018! 在没有power()函数的情况下运行时,它运行良好!!

#include <string>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <cmath>
#include <numeric>

using namespace std;

///the offending function///
double power(double base, int exponent)
{
    double product;
    //double base; int exponent;
    std::cout << "enter a value for base: " << endl;
    std::cin >> base;
    std::cout << "enter exponenent: " << endl;
    std::cin >> exponent;
    double result = 1;
    for (int i = 0; i < exponent; i++)
    {
        result = result * base;
        //product = base  exponent;
    }
    std::cout << product;
    return product;
}

///after here, things run fine if you X out the aforementioned function! Wow!

void exam()
{
    std::vector<int> scores;
    int F;
    F = 0; //string names;
    std::cout << "enter exam scores int:" << endl;

    //std::vector <string> names;
    while (F != -1)
    {
        std::cout << "Enter a new exame score:" << endl;
        std::cin >> F;
        scores.push_back(F);
    }
    if (F == -1)
    {
        std::cout << "end of score entering" << endl;
    }
    for (int i = 0; i < scores.size(); i++)
    {
        std::cout << scores[i];
    }

    /*
     while (i < scores.size())
     {
     std::cout << scores[i];
     i++;
     }
     */
    std::cout << "yay you made this work!!!!!!!!!!!!!" << endl;

}

int multiply()
{
    int a;
    int b;
    a = 8;
    b = 4;
    std::cout << a * b << endl;

    std::cout << "f*** yeah" << endl << endl;
    return 0;
}

void test()
{
    std::vector<int> newvector;
    int T;
    std::cout << "enter vector variables: " << endl;
    std::cin >> T;
    newvector.push_back(T);
    while (T != -1)
    {
        std::cout << "enter new vector variables T " << endl;
        std::cin >> T;
        newvector.push_back(T);
        if (T == -1)
        {
            newvector.pop_back();
        }
    }
    std::cout << "end of NewVector data inputs:" << endl;
    for (int W = 0; W < newvector.size(); W++)
    {
        std::cout << newvector[W] << endl;
    }
}

int main()
{

    power(2, 3);
    exam();
    /*int result = multiply();
     std::cout << "endl ;" << endl;
     test();
     system("pause"); */
    multiply();
    string name;
    int a;
    std::cout << "enter a variable for your name: " << endl;
    std::getline(cin, name);
    if (name == "aaron")
    {
        std::cout << " what a dumb name, aAron?" << endl;
    }
    else if (name == "todd")
    {
        std::cout << "what a dottly name, Todd" << endl;
    }
    else
    {
        std::cout << "your name = " << name << endl;
    }

    //std::vector <string>

    std::vector<int> asdf;
    std::cout << "enter an int for a" << endl;
    std::cin >> a;
    asdf.push_back(a);
    while (a != -1)
    {
        std::cout << "enter another A: " << endl;
        std::cin >> a;
        asdf.push_back(a);
        if (a == -1)
        {
            asdf.pop_back();
        }
    } //set var; checks if d<size(); if so, JUMP to std::cout<<; when finished with body, find  after size(); == "d++", then refer back to declaration)

    /*/ for(int G = 0; G<asdf.size(); G++)
     {
     std::cout << asdf[G] << endl;
     } */

    for (int i = 0; i < asdf.size(); i++)
    {

        std::cout << asdf[i] << "f*** it works!!!!!! " << endl;
    }

    for (int d = 0; d < asdf.size(); d++)
    { //htt ps://youtu.be/_1AwR-un4Hk?t=155

        std::cout << asdf[d] << ", ";
    }
    std::cout << endl;
    std::cout << std::accumulate(asdf.begin(), asdf.end(), 0);
    //std::cout<<

    system("pause");
    return 0;

}

power函数的存在应该对这个问题没有影响。 可能您没有看到警告,因为没有power功能,程序无法编译。

for (int W = 0; W < newvector.size(); W++)

newvector.size()返回一个无符号整数 int W是有符号整数。 你得到的正是你所要求的。

您可以将int W更改为vector<int>::size_type W (但不太详细的size_t W也应该有效)以使错误消息消失,但这是一个错误,您可能必须添加超过 20 亿个项目到vector查看清单。

解决方案:

for (vector<int>::size_type W = 0; W < newvector.size(); W++)

然而,这是一个基于范围的for循环的好地方

for (const auto &val: newvector) 
{ 
    std::cout << val << endl; 
} 

通过让编译器计算出所有的大小和类型,您的生活会轻松得多。

这在整个代码中重复了几次。

回复: WHEN RUN, It makes error C4018 -

  1. 你犯了那个错误(实际上是警告),而不是“它”。
  2. 该警告是由编译器报告的,因此您还没有运行任何东西...
  3. 您新添加的函数使用未初始化的变量product 在我的 Visual Studio 版本中,这是一个错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM