使CppUnit在netbeans 7.2上读取应用程序类

Getting CppUnit to read application class on netbeans 7.2

本文关键字:读取 应用程序 CppUnit netbeans      更新时间:2023-10-16

我正在同时学习c++和CppUnit,使用netbeans 7.2。

创建如下文件

#include <cstdlib>
using namespace std;
/*
 * 
 */
class Subtract{
public:
    int minus(int a, int b){
        return a-b;
    }
};
int main(int argc, char** argv) {
    return 0;
}
然后右键单击生成以下cppunit测试文件
#include "newtestclass.h"

CPPUNIT_TEST_SUITE_REGISTRATION(newtestclass);
newtestclass::newtestclass() {
}
newtestclass::~newtestclass() {
}
void newtestclass::setUp() {
}
void newtestclass::tearDown() {
}
int Subtract::minus(int a, int b);
void newtestclass::testMinus() {
    int a=89;
    int b=55;
    Subtract subtract;
    int result = subtract.minus(a, b);
    CPPUNIT_ASSERT_EQUAL(34,result);
}

当我尝试运行测试时,它给出以下错误

g++    -c -g -I. -MMD -MP -MF build/Debug/GNU-MacOSX/tests/tests/newtestclass.o.d -o build/Debug/GNU-MacOSX/tests/tests/newtestclass.o tests/newtestclass.cpp
tests/newtestclass.cpp:25: error: 'Subtract' has not been declared
tests/newtestclass.cpp: In member function 'void newtestclass::testMinus()':
tests/newtestclass.cpp:30: error: 'Subtract' was not declared in this scope
tests/newtestclass.cpp:30: error: expected `;' before 'subtract'
tests/newtestclass.cpp:31: error: 'subtract' was not declared in this scope
make[1]: *** [build/Debug/GNU-MacOSX/tests/tests/newtestclass.o] Error 1
make: *** [.build-tests-impl] Error 2

如何使其正常工作?

在c++中,惯例是在头文件(.h文件)中声明类和函数,并在源文件(.cpp文件)中实现它们。

你的Subtract.h文件(declarations)应该只有这样:

class Subtract {
public:
    int minus(int a, int b);
};

你的Subtract.cpp文件(实现)应该有这样的内容:

#include "Subtract.h"
int Subtract::minus(int a, int b)
{
    return a-b;
}

然后在newtestclass.cpp文件中#include "Subtract.h"