2

関数からベクトルを返そうとしていますが、いくつか問題があるようです....

現在、これは私が持っている最高のものです:

int* OptimizedSkillLevels(int skillLevel, const int numSkills, int duration, FragmentOptimizationParameters FOP){

        //some code to work with some things was here

        vector<int> skills(numSkills);

        int skillIncrement = 0;
        while (skillLevel > FOP.levelDifference){
        skills[skillIncrement%numSkills] += FOP.levelDifference;
        skillLevel-= FOP.levelDifference;
        skillIncrement++;
        }
        skills[skills.size()-1] += skillLevel;

        //at this point, the vector skills is full of the correct values, which is what really matters.
    }

    int* skillsArr = &skills[0];
    return skillsArr;
}

この関数呼び出しからアクセスすると、ガベージ値が表示されるようですが:

cout << OptimizedSkillLevels(skillLevels[i], userMaxNumSkills, durations[i], FOPs[FOPindex])[j] << ", ";

具体的には-17891602、何であれ、毎回そこから得てい[j]ます。

そこで、次のようにベクトルに変換しようとしました。

vector<int> OptimizedSkillLevels(int skillLevel, const int numSkills, int duration, FragmentOptimizationParameters FOP){

        //some code to work with some things was here

        vector<int> skills(numSkills);

        int skillIncrement = 0;
        while (skillLevel > FOP.levelDifference){
        skills[skillIncrement%numSkills] += FOP.levelDifference;
        skillLevel-= FOP.levelDifference;
        skillIncrement++;
        }
        skills[skills.size()-1] += skillLevel;

        //at this point, the vector skills is full of the correct values, which is what really matters.
    }

    return skills;
}

関数プロトタイプを使用:

vector<int> OptimizedSkillLevels(int skillLevel, int numSkills, int duration, FragmentOptimizationParameters FOP);

しかし、これを含む多数のエラーが発生しています(プロトタイプの行で):

1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(59): error C2143: syntax error : missing ';' before '<'
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(59): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

そして、これらは、関数の戻り値の型をベクトルに変更する前に表示されたものはありません...だから、何が起こっているのか本当にわかりません。

    1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(59): error C2143: syntax error : missing ';' before '<'
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(59): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(73): error C2872: 'vector' : ambiguous symbol
1>          could be 'c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(59) : int vector'
1>          or       'c:\program files (x86)\microsoft visual studio 11.0\vc\include\vector(655) : std::vector'
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(73): error C2143: syntax error : missing ';' before '<'
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(73): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(73): error C2086: 'int vector' : redefinition
1>          c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(59) : see declaration of 'vector'
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(74): error C2872: 'vector' : ambiguous symbol
1>          could be 'c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(59) : int vector'
1>          or       'c:\program files (x86)\microsoft visual studio 11.0\vc\include\vector(655) : std::vector'
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(74): error C2143: syntax error : missing ';' before '<'
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(74): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(74): error C2086: 'int vector' : redefinition
1>          c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(59) : see declaration of 'vector'
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(84): error C2065: 'FOPs' : undeclared identifier
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(84): error C2228: left of '.push_back' must have class/struct/union
1>          type is ''unknown-type''
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(85): error C2065: 'FOPs' : undeclared identifier
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(85): error C2228: left of '.push_back' must have class/struct/union
1>          type is ''unknown-type''
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(86): error C2065: 'FOPs' : undeclared identifier
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(86): error C2228: left of '.push_back' must have class/struct/union
1>          type is ''unknown-type''
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(87): error C2065: 'FOPs' : undeclared identifier
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(87): error C2228: left of '.push_back' must have class/struct/union
1>          type is ''unknown-type''
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(88): error C2065: 'FOPs' : undeclared identifier
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(88): error C2228: left of '.push_back' must have class/struct/union
1>          type is ''unknown-type''
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(89): error C2065: 'FOPs' : undeclared identifier
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(89): error C2228: left of '.push_back' must have class/struct/union
1>          type is ''unknown-type''
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(91): error C2065: 'FCPs' : undeclared identifier
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(91): error C2228: left of '.push_back' must have class/struct/union
1>          type is ''unknown-type''
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(92): error C2065: 'FCPs' : undeclared identifier
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(92): error C2228: left of '.push_back' must have class/struct/union
1>          type is ''unknown-type''
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(93): error C2065: 'FCPs' : undeclared identifier
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(93): error C2228: left of '.push_back' must have class/struct/union
1>          type is ''unknown-type''
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(94): error C2065: 'FCPs' : undeclared identifier
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(94): error C2228: left of '.push_back' must have class/struct/union
1>          type is ''unknown-type''
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(95): error C2065: 'FCPs' : undeclared identifier
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(95): error C2228: left of '.push_back' must have class/struct/union
1>          type is ''unknown-type''
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(96): error C2065: 'FCPs' : undeclared identifier
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(96): error C2228: left of '.push_back' must have class/struct/union
1>          type is ''unknown-type''
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(169): warning C4244: 'return' : conversion from 'const double' to '__int64', possible loss of data
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(192): warning C4244: 'argument' : conversion from 'double' to '__int64', possible loss of data
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(195): warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(196): warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(197): warning C4244: 'argument' : conversion from 'double' to '__int64', possible loss of data
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(202): warning C4244: '+=' : conversion from 'double' to 'int', possible loss of data
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(203): warning C4244: '+=' : conversion from 'double' to 'int', possible loss of data
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(215): error C2872: 'vector' : ambiguous symbol
1>          could be 'c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(59) : int vector'
1>          or       'c:\program files (x86)\microsoft visual studio 11.0\vc\include\vector(655) : std::vector'
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(215): error C2062: type '__int64' unexpected
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(216): error C2872: 'vector' : ambiguous symbol
1>          could be 'c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(59) : int vector'
1>          or       'c:\program files (x86)\microsoft visual studio 11.0\vc\include\vector(655) : std::vector'
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(216): error C2062: type '__int64' unexpected
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(217): error C2872: 'vector' : ambiguous symbol
1>          could be 'c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(59) : int vector'
1>          or       'c:\program files (x86)\microsoft visual studio 11.0\vc\include\vector(655) : std::vector'
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(217): error C2062: type '__int64' unexpected
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(229): error C2065: 'optimalCosts' : undeclared identifier
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(229): error C2228: left of '.size' must have class/struct/union
1>          type is ''unknown-type''
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(231): error C2065: 'optimalCosts' : undeclared identifier
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(231): error C2228: left of '.resize' must have class/struct/union
1>          type is ''unknown-type''
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(232): error C2065: 'skillLevels' : undeclared identifier
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(232): error C2228: left of '.resize' must have class/struct/union
1>          type is ''unknown-type''
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(233): error C2065: 'durations' : undeclared identifier
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(233): error C2228: left of '.resize' must have class/struct/union
1>          type is ''unknown-type''
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(236): error C2065: 'optimalCosts' : undeclared identifier
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(239): error C2065: 'optimalCosts' : undeclared identifier
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(239): error C2228: left of '.size' must have class/struct/union
1>          type is ''unknown-type''
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(242): error C2065: 'optimalCosts' : undeclared identifier
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(243): error C2065: 'optimalCosts' : undeclared identifier
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(244): error C2065: 'skillLevels' : undeclared identifier
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(245): error C2065: 'durations' : undeclared identifier
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(251): error C2065: 'optimalCosts' : undeclared identifier
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(251): error C2228: left of '.size' must have class/struct/union
1>          type is ''unknown-type''
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(252): error C2065: 'optimalCosts' : undeclared identifier
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(252): error C2065: 'skillLevels' : undeclared identifier
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(252): error C2065: 'durations' : undeclared identifier
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(265): error C2872: 'vector' : ambiguous symbol
1>          could be 'c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(59) : int vector'
1>          or       'c:\program files (x86)\microsoft visual studio 11.0\vc\include\vector(655) : std::vector'
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(265): error C2062: type 'int' unexpected
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(266): error C2872: 'vector' : ambiguous symbol
1>          could be 'c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(59) : int vector'
1>          or       'c:\program files (x86)\microsoft visual studio 11.0\vc\include\vector(655) : std::vector'
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(266): error C2062: type 'int' unexpected
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(269): error C2065: 'optimalCosts' : undeclared identifier
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(269): error C2228: left of '.size' must have class/struct/union
1>          type is ''unknown-type''
1>c:\users\vasu\documents\visual studio 11\projects\composing calc\composing calc\composing calc.cpp(269): fatal error C1903: unable to recover from previous error(s); stopping compilation
1>

ベクトルを返す適切な方法または何らかの回避策について、誰かが提供できる助けをいただければ幸いです。

4

1 に答える 1