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

5

您的第一种方法返回一个指向不再存在的对象的指针。这显然永远不会奏效。

你的第二种方法是正确的。只需修复语法错误。你有#include <vector>吗?你有using namespace std;吗?

于 2013-09-15T03:56:54.723 回答