![](/img/trans.png)
[英]warning C4018: '>=' : signed/unsigned mismatch in VSC++ 2010
[英]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
-
product
; 在我的 Visual Studio 版本中,这是一个错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.