C++ 找不到错误

C++ Can't find error

本文关键字:错误 找不到 C++      更新时间:2023-10-16

不确定我做错了什么。我跑不动了。

    #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;
}