私はC++を初めて使用し、次のコードを記述しています。呼び出し元の関数のすべてのアドオンを反復処理する必要がありました- testFunction
。これはC#で機能することは知っていますが、このコードは機能していません。誰かがC++でそれを行う正しい方法を指摘できますか?
#include "stdafx.h"
#include <iostream>
#include "resource.h"
int testFunction(char* tester);
int _tmain()
{
int mainProd=2;
int Addons[]={7,8,9,10};
testFunction(mainProd,Addons);
}
void testFunction(int mainProd,int addons[])
{
for(int x = 0 ; addons.length;++x) ---- Not working
{
std::cout<< addons[x];
}
}
私はあなたたちによる以下の提案としてベクトルを実装しようとしました
#include "stdafx.h"
#include <iostream>
#include "resource.h"
#include <vector>
void testFunction(std::vector<int> addons);
int _tmain(int argc, _TCHAR* argv[])
{
std::vector<int> Addons ;
for(int i = 0 ;i<10;++i)
{
Addons.push_back(i);
}
testFunction(Addons);
}
void testFunction(std::vector<int> addons)
{
for(int i =0 ; i<addons.size();++i)
{
std::cout<<addons.at(i);
}
}