Where to put main and what to write there? - c++

I have the following code in file tested.cpp:
#include <iostream>
using namespace std;
class tested {
private:
int x;
public:
tested(int x_inp) {
x = x_inp;
}
int getValue() {
return x;
}
};
I also have another file (called testing.cpp):
#include <cppunit/extensions/HelperMacros.h>
#include "tested.cpp"
class TestTested : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE(TestTested);
CPPUNIT_TEST(check_value);
CPPUNIT_TEST_SUITE_END();
public:
void check_value();
};
CPPUNIT_TEST_SUITE_REGISTRATION(TestTested);
void TestTested::check_value() {
tested t(3);
int expected_val = t.getValue();
CPPUNIT_ASSERT_EQUAL(7, expected_val);
}
When I try to compile the testing.cpp file I get: undefined reference tomain'`. Well, this is because I do not have main (the entry point for the program). So, the compiler does not know how to start the execution of the code.
But what is not clear to me is how to execute the code in the testing.cpp. I tried to add:
int main() {
TestTested t();
return 1;
}
However, it does not print anything (and it is expected to return an error message since 3 is not equal to 7).
Does anybody know what is the correct way to run the unit test?

Since you are writing a cppunit test, why not looking at cppunit doc ? (http://cppunit.sourceforge.net/doc/lastest/cppunit_cookbook.html)
It tells you that the main sould be written like this :
#include <cppunit/ui/text/TestRunner.h>
#include "ExampleTestCase.h"
#include "ComplexNumberTest.h"
int main( int argc, char **argv) {
CppUnit::TextUi::TestRunner runner;
runner.addTest( ExampleTestCase::suite() );
runner.addTest( ComplexNumberTest::suite() );
runner.run();
return 0;
}

Related

About compilation unit and Schwarz Counter and singleton pattern

I look this example and practice it:
https://novus.pixnet.net/blog/post/23784820
(The code is at the end)
The compile command is:
g++ sc.cc main.cc -o main.exe
I have 2 questions:
The Compilation Unit has 2 unit which consist of sc and main. (correct?)
I know this example (Schwarz Counter) is applied to iostream (C++).Singleton pattern looks suitable for this situation. Does Singleton pattern is better than Schwarz Counter in the case for unique and global (such as file io)?
Code:
sc.h
#include <fstream>
class Log {
friend class Initializer;
public:
void Print(const char* msg) {
std::fputs(msg, onlyFile_);
}
private:
static std::FILE* onlyFile_;
};
class Initializer {
public:
Initializer();
~Initializer();
private:
static int ref_;
};
extern Log log;
static Initializer init;
#endif
sc.cc
#include "s.h"
std::FILE* Log::onlyFile_ = 0;
int Initializer::ref_ = 0;
Initializer::Initializer() {
if (0 == ref_++) {
Log::onlyFile_ = fopen("somefile.txt", "w");
}
}
Initializer::~Initializer() {
if (0 == --ref_) {
fclose(Log::onlyFile_);
}
}
main.c
#include "s.h"
int main(int argc, char const *argv[]) {
log.Print("aa");
return 0;
}

Unable to create SDL thread with multi file program

I'm having a problem with SDL threads so I made a little multi file code so it will be easier to show my problem
Header file
#ifndef MAINC_H_INCLUDED
#define MAINC_H_INCLUDED
#include <iostream>
#include <CONIO.H>
#include <SDL.h>
#include <SDL_thread.h>
using namespace std;
class mainc
{
private:
SDL_Thread* thread;
int threadR;
int testN=10;
public:
int threadF(void *ptr);
int OnExecute();
bool start();
};
#endif
One file
#include "mainc.h"
bool mainc::start() {
if(SDL_Init(SDL_INIT_EVERYTHING) < 0) {
return false;
}
getch();
if(SDL_CreateThread(threadF, "TestThread", (void *)NULL)==NULL){
return false;
}
return true;
}
int mainc::threadF(void *ptr){
cout<<"This is a thread and here is a number: "<<testN<<endl;
return testN;
}
Second file
#include "mainc.h"
int mainc::OnExecute() {
if(!start())
{
return -1;
}
SDL_WaitThread(thread,&threadR);
return 0;
}
int main(int argc, char* argv[]) {
mainc game;
return game.OnExecute();
}
When I compile this I get this error
cannot convert 'mainc::threadF' from type 'int (mainc::)(void*)' to type 'SDL_ThreadFunction {aka int (*)(void*)}'
I dug around a bit and I found a solution but it gave me other errors I needed to make threadF static but I couldn't access any variables it gave me this error
invalid use of member 'mainc::testN' in static member function
But if I remove the variable from the function it runs fine
Now I don't know what do to because in my game I need to share variables which change
testN is neither a static nor a public property of class mainc and to do what you're trying to do, it needs to be either.
If you want to use members of class "mainc" from within another thread body, you need to pass a pointer to an object of class "mainc" to SDL_CreateThread:
// ...
SDL_CreateThread(threadF, "TestThread", this)
// ...
and then
int mainc::threadF(void *ptr)
{
mainc* myMainc = (mainc*)ptr;
myMainc->testN; // now you can use it as you need
}
Beware of the encapsulation, though (testN is actually private)

What does "void-value is not ignored" error mean and how to remove it?

I try to compile the following code:
#include <cppunit/extensions/HelperMacros.h>
#include "tested.h"
class TestTested : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE(TestTested);
CPPUNIT_TEST(check_value);
CPPUNIT_TEST_SUITE_END();
public:
void check_value();
};
CPPUNIT_TEST_SUITE_REGISTRATION(TestTested);
void TestTested::check_value() {
tested t(3);
int expected_val = t.getValue(); // <----- Line 18.
CPPUNIT_ASSERT_EQUAL(7, expected_val);
}
As a result I get:
testing.cpp:18:32: Error: void-value is not ignored where it should be
EDDIT
To make the example complete I post the code of the tested.h and tested.cpp:
tested.h
#include <iostream>
using namespace std;
class tested {
private:
int x;
public:
tested(int int_x);
void getValue();
};
tested.cpp
#include <iostream>
using namespace std;
tested::tested(int x_inp) {
x = x_inp;
}
int tested::getValue() {
return x;
}
you declare void getValue(); in the class tested.. change to int getValue();.
A void function cannot return a value.
You are getting a value of int from the API getValue(), hence it should return an int.
Your class definition doesn't match the implementation:
In your header you've declared it in the following way (as an aside, you might want to look into some naming conventions).
class tested {
private:
int x;
public:
tested(int int_x);
void getValue();
};
You've declared getValue() as void, i.e no return. Doesn't make much sense for a getter to return nothing, does it?
However, in the .cpp file you've implemented getValue() like so:
int tested::getValue() {
return x;
}
You need to update the getValue() method signature in the header type so that its return type matches the implementation (int).

Getting an undefined reference error

I am hoping someone can point me in the right direction to figure out why I am getting the following error:
$~/display/triangleDisplayable.cc:4: undefined reference to `Displayable::Displayable()'
I am trying to abstract a class Displayable and have a class triangleDisplayable that implements its methods. The two header files I have are "Displayable.h":
class Displayable {
public:
Displayable();
virtual int getSizeOfArrays() = 0;
void display(int size);
private:
virtual void init() = 0;
virtual int getSizeOfPointsArray() = 0;
virtual int getSizeOfNormalsArray() = 0;
};
and "triangleDisplayable.h"
#include "Displayable.h"
class triangleDisplayable : public Displayable
{
public:
triangleDisplayable();
int getSizeOfArrays();
private:
void init();
int getSizeOfPointsArray();
int getSizeOfNormalsArray();
};
And then I have "Displayable.cc"
#include <iostream>
#include "Displayable.h"
Displayable::Displayable() {
std::cout << "testing Displayable constructor" << std::endl;
}
void Displayable:display(int size) {
}
int main () {
return 0;
}
and "triangleDisplayable.cc"
#include <iostream>
#include "triangleDisplayable.h"
triangleDisplayable::triangleDisplayable() : Displayable() {
}
int triangleDisplayable::getSizeOfArrays() {
return 0;
}
void triangleDisplayable::init() {
}
int triangleDisplayable::getSizeOfPointsArray() {
return 0;
}
int triangleDisplayable::getSizeOfNormalsArray() {
return 0;
}
int main () {
return 0;
}
I have been trying to follow along with various tutorials to learn how to do abstraction in C++, but I have not really been able to find any helpful solutions to this. I believe that all of my #includes are correct, which I read is a common problem. The error message seems to indicate that the problem is the line
triangleDisplayable::triangleDisplayable() : Displayable() {
}
I have tried to compile without the : Displayable() but I get the same error. Is there perhaps a problem with my syntax in my header files?
No, the error is in tool invocation. You need to link the two source files together (e.g. g++ -o foo a.cc b.cc). And remove one of the main functions, as you can't have two different ones.

Question about implementing abstract functions in C++?

I am learning and testing a piece of C++ code as follows:
#include "stdafx.h"
#include <iostream>
using namespace std;
#include <conio.h>
#include <cstring>
class Shape {
public:
Shape() {};
~Shape() {};
virtual void display() const = 0;
virtual double volume() const = 0;
};
class Square : public Shape {
public:
Square() {};
~Square() {};
void display() const;
double volume() const;
};
void Square::display() const {
cout << "Square!!!!!!!!!!!!!!" << endl;
}
double Square::volume() const {
cout << "Square Volume........." << endl;
return 0.0;
}
int _tmain(int argc, _TCHAR* argv[])
{
Shape *s;
s = new Square; // error here
(*s).display();
return 0;
}
The above code does not compile successfully. it produces: "fatal error LNK1120: 1 unresolved externals".
Can anyone help me out with that?
I am using MS VS C++ 2005.
Thanks
The above code compiles and runs properly on VS 2010 as well as Ideone.
Check this
There is nothing wrong in the way you have implemented your abstract functions in the above snippet.
I'm pretty sure your problem is your main declaration.
If you change it to a standard main definition, I believe your linking problems will be fixed.
int main()
{
Shape *s = new Square(); // error here
s->display();
return 0;
}