【发布时间】:2018-06-25 15:23:11
【问题描述】:
我正在尝试在 Visual Studio 2010 中为一个非常基本的 C++ 类进行基本单元测试。我已经测试了该类并且一切正常。但是,当我做一个测试项目时,我无法让类被识别。
我的班级头文件:
#include <string>
#include <iostream>
using namespace std;
#ifndef FIRST_H
#define FIRST_H
class First
{
private:
string name;
int age;
public:
// Constructors
First(); // default constructor
First(string n, int a); // constructor with parameters
void ChangeAge(int newAge);
void ChangeName(string newName);
void getName();
void getAge();
};
#endif
我的班级 cpp 文件:
#include "First.h"
// Constructors
First :: First()// default constructor
{
name = "No Name";
age = 0;
}
First :: First(string n, int a) // constructor with parameters
{
name = n;
age = a;
}
//Manipulators
void First :: ChangeAge(int newAge)
{
age = newAge;
}
void First :: ChangeName(string newName)
{
name = newName;
}
// Observers
void First :: getName()
{
cout << "The name of this student is " << name << endl;
//cin >> name;
}
void First :: getAge()
{
cout << "The age of this student is " << age << endl;
}
这是我的基本单元测试: // 注意:所有这些都是自动生成的,除了 void TestMethod1() 的主体
#include "stdafx.h"
using namespace System;
using namespace System::Text;
using namespace System::Collections::Generic;
using namespace Microsoft::VisualStudio::TestTools::UnitTesting;
namespace TestProject5
{
[TestClass]
public ref class UnitTest1
{
public:
[TestMethod]
void TestMethod1()
{
First person; // Sets Age to 0
person.getAge(); // Should Display 0
}
};
}
在构建单元测试项目时,我收到一条错误消息: “First 是一个未声明的标识符”。
如果我将“public ref class”值从“UnitTest1”更改为我的类名“First”,我会收到以下错误: "'getAge' 不是 'TestProject5::First' 的成员"
【问题讨论】:
-
请不要发布您的代码或错误消息的图片,但在您的问题中包含所有相关信息作为文本。
-
您可能缺少
using namespace指令。还要确保 First 类实际上是 C++/CLI 类(一个 ref 类)并且它是公共的。而且您实际上打算创建一个托管单元测试,C++/CLI 类不适合“非常基本的 C++ 类”这个绰号。 -
这是你的代码,我看不到。使用对象浏览器。
-
@HansPassant 我的“第一”类不是 Ref 类。我需要做什么来修复它? --我不知道 ref 类是什么
-
@Capricorn 我现在有代码。对此感到抱歉
标签: c++ unit-testing visual-studio-2010