C++ 找不到错误
C++ Can't find error
不确定我做错了什么。我跑不动了。
#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int a[] = { 2, 3, 4, 5 };
skipOne(a, 2);
}
void skipOne(int * array, int n)
{
int total = 0;
for (int i = 0; i < sizeof(array); i++)
{
if (i != n)
{
total = + array[i];
}
}
cout << "The total is: " << total << endl;
}
这就是我在下面你的帮助下所做的。
#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
void skipOne(vector<int> & array, int n)
{
int total = 1;
for (int i = 0; i < array.size(); i++)
{
if (i != n)
{
total *= array[i];
}
}
cout << "The total is: " << total << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
vector<int> a = { 2, 3, 4, 5 };
skipOne(a, 0);
skipOne(a, 1);
skipOne(a, 2);
skipOne(a, 3);
}
sizeof
不适用于传递的数组或指针。您必须将数组长度作为参数传递。
此外,在使用函数之前必须声明它们。
void skipOne(int * array, int n, int length); // declare here
int _tmain(int argc, _TCHAR* argv[])
{
int a[] = { 2, 3, 4, 5 };
skipOne(a, 2, 4);
}
void skipOne(int * array, int n, int length)
{
int total = 0;
for (int i = 0; i < length; i++) // use length here
{
if (i != n)
{
total += array[i]; // += instead of = +
}
}
cout << "The total is: " << total << endl;
}
sizeof(array),它等效于sizeof(int*),因为数组的类型是int*,通常等于4或8,具体取决于所使用的系统。它与数组a中的元素数量没有任何共同之处。程序中使用的任何名称也应在使用前进行定义。您在main中使用了函数skipOne,但它尚未声明。
因此,有效代码可以按照以下方式
#include "stdafx.h"
#include <iostream>
int skipOne( const int *, int, int );
int _tmain(int argc, _TCHAR* argv[])
{
int a[] = { 2, 3, 4, 5 };
std::cout << "The total is: " << skipOne( a, sizeof( a ) / sizeof( *a ), 2 )
<< std::endl;
}
int skipOne( const int * array, int n, int k )
{
int total = 0;
for ( int i = 0; i < n; i++ )
{
if ( i != k )
{
total += array[i];
}
}
return total;
}
您还可以使用标准容器std::array。在这种情况下,代码看起来会更简单。例如
#include "stdafx.h"
#include <iostream>
#include <array>
int skipOne( const std::array<int, 4> &, int );
int _tmain(int argc, _TCHAR* argv[])
{
std::array<int, 4> a = { 2, 3, 4, 5 };
std::cout << "The total is: " << skipOne( a, 2 )
<< std::endl;
}
int skipOne( const std::array<int, 4> &a, int k )
{
int total = 0;
for ( int i = 0; i < a.size(); i++ )
{
if ( i != k )
{
total += a[i];
}
}
return total;
}
您需要提前声明skipOne并使用vector.size()
#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
void skipOne(int * array, int n);
int _tmain(int argc, _TCHAR* argv[])
{
vector<int> a = { 2, 3, 4, 5 };
skipOne(a, 2);
}
void skipOne(vector<int> & array, int n)
{
int total = 0;
for (int i = 0; i < array.size(); i++)
{
if (i != n)
{
total = + array[i];
}
}
cout << "The total is: " << total << endl;
}
相关文章:
- 静态链接库时,收到链接器错误:找不到 -lgcc_s
- C++ ZBar cmake 错误"找不到文件"时包括
- GTK+3 编译错误"找不到信号处理程序",您是否使用 -rdynamic 进行了编译?
- SWIG c++ 到 C# 错误找不到入口点SWIGRegisterExceptionCallbacks_xxxx
- NDK R20 LD:LD:错误:找不到-lpthread
- 双重释放或损坏错误(找不到错误?
- 错误找不到-lglu
- 错误:找不到匹配的超载函数
- 生成文件错误 - 找不到文件 - *.cpp
- Qt 创建器错误:找不到 -lopencv_imgcodecs
- 无法配置 AGENT++ 配置: 错误: 找不到合适的 libsnmp++ 库
- CMake 错误:找不到变量.ACE_INCLUDE_DIR(高级)
- CMake 包含错误(找不到文件、未定义引用)
- 链接错误:找不到__thiscall函数符号,但定义了__cdecl函数符号
- 运行时错误:找不到与 ImageSensor 匹配的 Python 模块
- 编译错误:找不到库
- CMake错误:找不到包含目录和库
- Mac OSX:构建opencv_contrib致命错误:找不到文件 opencv2/core.hpp
- Qt 创建者编译错误"找不到 UIC.exe从 msvc 2012 路径"
- Ubuntu - 链接 boost.python - 致命错误:找不到 pyconfig