1
#include "stdafx.h"
#include "CppUnitTest.h"
#include <iostream>
#include <cstdlib>
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace UnitTest2
{
TEST_CLASS(UnitTest1)
{
public:
int vector [6] = { 14, 10, 11, 19, 2, 25 };
bool ArrayAreEqual;
int static compare(const void * x1, const void * x2)
{
return (*(int*)x1 - *(int*)x2);
}
TEST_METHOD(TestMethod1)
{
qsort(vector, 6, sizeof(int), compare);
for (int ix = 0; ix < 6; ix++)
std::cout << vector[ix] << " ";
// TODO: Your test code here
int TestVector[6] = { 2,10,11,14,19,25 };
if (std::equal(std::begin(vector), std::end(vector), std::begin(TestVector)))
{
ArrayAreEqual = true;
}
else
{
ArrayAreEqual = false;
}
Assert::IsTrue(ArrayAreEqual);
}
};
}
在我的代码int vector [6] = {14,10,11,19,2,25};只能用元素个数定义[6](否则显示不完整的类型是不允许的),但是如果这个变量是全局的,它可以被定义为没有大小,如int vector [] = {14,10,11,19 ,2,25}; 这是为什么发生?全局变量和内部类有什么区别?
请推荐这个问题的另一个标题。 –
它的所有代码在问题中。最后VS 2017.刚取代int vector [6] = {14,10,11,19,2,25}; int vector [] = {14,10,11,19,2,25};并获得不合格的类型是不允许的。 –
这是因为类成员变量初始化与其定义非常分离。初始化发生在创建类的对象(实例)时(将它想象成构造函数中的一种隐藏初始化器)。初始化也可以由构造函数覆盖。 –