I am learning C++ and CppUnit at the same time, using netbeans 7.2.
I create the following file
#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;
}
And then I right-click to generate the following cppunit test file
#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);
}
When I try to run the test, it gives the following errors
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
How do I get this to work properly?
In C++, the convention is to declare the classes and functions in a header file (.h file) and implement them in the source file (.cpp file).
Your Subtract.h file (declarations) should have only this:
class Subtract {
public:
int minus(int a, int b);
};
Your Subtract.cpp file (implementation) should have this:
#include "Subtract.h"
int Subtract::minus(int a, int b)
{
return a-b;
}
Then you #include "Subtract.h" in your newtestclass.cpp file.
Related
I am a C ++ beginner and am looking to create a module. I have followed several guides and would like to test modules with classes. When I try to run the first module via g++-11 -c -std=c++20 -fmodules-ts func.cxx i get the following error:
In file included from /usr/local/Cellar/gcc/11.2.0_3/include/c++/11/bits/stl_iterator.h:82,
from /usr/local/Cellar/gcc/11.2.0_3/include/c++/11/bits/stl_algobase.h:67,
from /usr/local/Cellar/gcc/11.2.0_3/include/c++/11/bits/char_traits.h:39,
from /usr/local/Cellar/gcc/11.2.0_3/include/c++/11/string:40,
from func.cxx:2:
/usr/local/Cellar/gcc/11.2.0_3/include/c++/11/new:89:27: error: cannot define 'enum class std::align_val_t' in different module
89 | enum class align_val_t: size_t {};
| ^~~~~~
<built-in>: note: declared here
/usr/local/Cellar/gcc/11.2.0_3/include/c++/11/new:89: confused by earlier errors, bailing out
Below are the files, thanks in advance.
main.cpp
#include <iostream>
import airline_ticket;
int main()
{
std::cout << "Hello" << std::endl;
return 0;
}
func.cxx
export module airline_ticket;
#include <string>
export class AirlineTicket
{
public:
AirlineTicket();
~AirlineTicket();
double calculatePriceInDollars(); std::string getPassengerName();
void setPassengerName(std::string name);
int getNumberOfMiles();
void setNumberOfMiles(int miles);
bool hasEliteSuperRewardsStatus();
void setHasEliteSuperRewardsStatus(bool status);
private:
std::string m_passengerName;
int m_numberOfMiles;
bool m_hasEliteSuperRewardsStatus;
};
func_impl.cxx
module airline_ticket;
AirlineTicket::AirlineTicket()
{
// Initialize data members.
m_passengerName = "Unknown Passenger";
m_numberOfMiles = 0;
m_hasEliteSuperRewardsStatus = false;
}
the include directive in func.cxx needs to be in the global module fragment area. Else you'll get redefinitions.
I.e.
module;
#include <string>
export module .....
...
I'm initiating to C++ and I'm struggling with a compiling problem
I have a source file "binomial.cpp" in which I define the methods of my classes :
#include "binomial.hpp"
using namespace std;
int Bernoulli::operator()(){
return (rand() < p*RAND_MAX) ? a : b;
};
int Binomial::operator()(){
int result(0);
for(int i(0);i<n;++i){
int a;
a = B();
result += a;
};
return result;
};
and a header file "binomial.hpp" where I declare all my classes :
#include <iostream>
#ifndef BINOMIAL
#define BINOMIAL
class RandVar {
virtual int operator()() =0;
};
struct Bernoulli : public RandVar {
Bernoulli(int a = -1,int b = 1, double p = 0.5) : a(a), b(b), p(p) {};
int operator()(){};
private:
int a,b;
double p;
};
class Binomial : public RandVar {
public:
Binomial(Bernoulli B, int n=2)
: B(B), n(n) {}
int operator()(){};
private:
Bernoulli B;
int n;
};
#endif
But when I try to compile that through g++ using the command : g++ -Wall binomial.cpp -o binomial those errors occur :
binomial.hpp: In member function ‘virtual int Bernoulli::operator()()’:
binomial.hpp:14:19: warning: no return statement in function returning non-void [-Wreturn-type]
int operator()(){};
^
binomial.hpp: In member function ‘virtual int Binomial::operator()()’:
binomial.hpp:26:19: warning: no return statement in function returning non-void [-Wreturn-type]
int operator()(){};
^
binomial.cpp: At global scope:
binomial.cpp:4:5: error: redefinition of ‘int Bernoulli::operator()()’
int Bernoulli::operator()(){
^~~~~~~~~
In file included from binomial.cpp:2:0:
binomial.hpp:14:6: note: ‘virtual int Bernoulli::operator()()’ previously defined here
int operator()(){};
^~~~~~~~
binomial.cpp:8:5: error: redefinition of ‘int Binomial::operator()()’
int Binomial::operator()(){
^~~~~~~~
In file included from binomial.cpp:2:0:
binomial.hpp:26:6: note: ‘virtual int Binomial::operator()()’ previously defined here
int operator()(){};
^~~~~~~~
I don't really know how to fix that so if someone can take some time to help a beginner it would be great !
You should replace both
int operator()(){};
with
int operator()();
in the header files. You meant to provide declarations, not definitions. To provide just a declaration (not provide the code right away), just drop the {}.
main.cc
#include <cerrno>
class A {
public:
int errno();
};
int A::errno()
{
return 0;
}
int main()
{
return 0;
}
compile main.cc with g++ report error:
In file included from /usr/include/c++/7/cerrno:42:0,
from 1.cc:1:
main.cc:8:8: error: expected unqualified-id before ‘(’ token
int A::errno()
But the same code compile successfully by g++6 and below
And the following code compile successfully by g++7
#include <cerrno>
class A {
public:
int errno()
{
return 0;
}
};
int main()
{
return 0;
}
So any ideas?
errno is a macro.
Quoting cppreference
macro which expands to POSIX-compatible thread-local error number variable
if you add an #undef errno in your sources it will compile but somehow defeats the purpose of errno.
You simply should not name a method errno.
To figure out why it works in one case and not in the other you need to expand the errno macro and see what was generated.
So here is the output or what the compiler tries to compile:
class A {
public:
int (*__errno_location ())(){return 0;}
};
class A {
public:
int(*foo()) ();
};
int A::(*__errno_location ())(){return 0;}
I have problem when compiling code below on aix machine with gcc compiler (version 4.7.3):
SomeThread.h
#ifndef SomeThread_H
#define SomeThread_H
class SomeThread {
public:
SomeThread(void);
virtual ~SomeThread(void);
void runThread();
}; // SomeThread
#endif // _SomeThread_H_
SomeThread.cpp
#include "SomeThread.h"
#include <thread>
#include <iostream>
using namespace std;
namespace {
void foo_thread_function() {
for (int i = 0; i < 10; ++i) {
cout << "Some threaded text" << endl;
}
}
}
SomeThread::SomeThread() {
} // SomeThread
SomeThread::~SomeThread() {
} // ~SomeThread
void SomeThread::runThread() {
thread foo_thread_01(foo_thread_function);
thread foo_thread_02(foo_thread_function);
thread foo_thread_03(foo_thread_function);
foo_thread_01.join();
foo_thread_02.join();
foo_thread_03.join();
}
The error which I get is following:
SomeThread.cpp: In member function 'void SomeThread::runThread()':
SomeThread.cpp:58:4: error: reference to 'thread' is ambiguous
In file included from /usr/include/sys/ptrace.h:28:0,
from /usr/include/sys/proc.h:42,
from /usr/include/sys/pri.h:43,
from /usr/include/sys/sched.h:38,
from /usr/include/sched.h:51,
from /opt/freeware/lib/gcc/powerpc-ibm-aix7.1.0.0/4.7.3/include-fixed/pthread.h:76,
from /opt/freeware/lib/gcc/powerpc-ibm-aix7.1.0.0/4.7.3/include/c++/powerpc-ibm-aix7.1.0.0/pthread/ppc64/bits/gthr-posix.h:41,
from /opt/freeware/lib/gcc/powerpc-ibm-aix7.1.0.0/4.7.3/include/c++/powerpc-ibm-aix7.1.0.0/pthread/ppc64/bits/gthr-default.h:30,
from /opt/freeware/lib/gcc/powerpc-ibm-aix7.1.0.0/4.7.3/include/c++/powerpc-ibm-aix7.1.0.0/pthread/ppc64/bits/gthr.h:150,
from /opt/freeware/lib/gcc/powerpc-ibm-aix7.1.0.0/4.7.3/include/c++/ext/atomicity.h:34,
from /opt/freeware/lib/gcc/powerpc-ibm-aix7.1.0.0/4.7.3/include/c++/memory:75,
from /opt/freeware/lib/gcc/powerpc-ibm-aix7.1.0.0/4.7.3/include/c++/thread:40,
from SomeThread.cpp:5:
/usr/include/sys/thread.h:105:8: error: candidates are: struct thread
In file included from SomeThread.cpp:5:0:
/opt/freeware/lib/gcc/powerpc-ibm-aix7.1.0.0/4.7.3/include/c++/thread:60:9: error: class std::thread
SomeThread.cpp:58:11: error: expected ';' before 'foo_thread_01'
SomeThread.cpp:59:4: error: reference to 'thread' is ambiguous
In file included from /usr/include/sys/ptrace.h:28:0,
from /usr/include/sys/proc.h:42,
from /usr/include/sys/pri.h:43,
from /usr/include/sys/sched.h:38,
from /usr/include/sched.h:51,
from /opt/freeware/lib/gcc/powerpc-ibm-aix7.1.0.0/4.7.3/include-fixed/pthread.h:76,
from /opt/freeware/lib/gcc/powerpc-ibm-aix7.1.0.0/4.7.3/include/c++/powerpc-ibm-aix7.1.0.0/pthread/ppc64/bits/gthr-posix.h:41,
from /opt/freeware/lib/gcc/powerpc-ibm-aix7.1.0.0/4.7.3/include/c++/powerpc-ibm-aix7.1.0.0/pthread/ppc64/bits/gthr-default.h:30,
from /opt/freeware/lib/gcc/powerpc-ibm-aix7.1.0.0/4.7.3/include/c++/powerpc-ibm-aix7.1.0.0/pthread/ppc64/bits/gthr.h:150,
from /opt/freeware/lib/gcc/powerpc-ibm-aix7.1.0.0/4.7.3/include/c++/ext/atomicity.h:34,
from /opt/freeware/lib/gcc/powerpc-ibm-aix7.1.0.0/4.7.3/include/c++/memory:75,
from /opt/freeware/lib/gcc/powerpc-ibm-aix7.1.0.0/4.7.3/include/c++/thread:40,
from SomeThread.cpp:5:
I compile the above files with following command line:
g++ -maix64 -DTARGET=target_thread -DGENDATE=04_01_2017 -DTT_LIB_DLLSUFFIX=\".so\" -DOSNAME=\"AIX\" -D_GNU_SOURCE -D_REENTRANT -DAIX -Wno-deprecated -I. -std=gnu++11 -maix64 -pthread -mminimal-toc -fpermissive -Wno-write-strings -Winvalid-offsetof -O3 -c -oSomeThread.o SomeThread.cpp
The problem is, that there are multiple implementations of thread with your compiler options and includes. Maybe it would be just enough to correct the code to this.
SomeThread.cpp
#include "SomeThread.h"
#include <thread>
#include <iostream>
//Stop using namespace std, please
namespace SomeNamespace {
void foo_thread_function() {
for (int i = 0; i < 10; ++i) {
cout << "Some threaded text" << endl;
}
}
}
SomeThread::SomeThread() {
} // SomeThread
SomeThread::~SomeThread() {
} // ~SomeThread
void SomeThread::runThread() {
std::thread foo_thread_01(SomeNamespace::foo_thread_function);
std::thread foo_thread_02(SomeNamespace::foo_thread_function);
std::thread foo_thread_03(SomeNamespace::foo_thread_function);
foo_thread_01.join();
foo_thread_02.join();
foo_thread_03.join();
}
Ambiguous means that there are multiple interpretations of the same word.
Example:
namespace Bla{
struct SomeStruct{
}
}
namespace Blub{
struct SomeStruct{
}
}
int main(){
using namespace Bla;
using namespace Blub;
SomeStruct ImAmbiguous; // Problem now, which struct should the compiler choose now?
Bla::SomeStruct structFromBla; //Now the compiler knows which struct should be choosen
return 0;
}
I am getting this error when I try to compile my class. I have made sure there is function prototypes and variables are initilized correctly, hopefully someone can help me figure out the problem
g++ -c main.cc
g++ -c BankControl.cc
g++ -c Bank.cc
g++ -c Account.cc
g++ -c View.cc
g++ -c AcctList.cc
g++ -c Customer.cc
g++ -c CustArray.cc
g++ -c Transaction.cc
Transaction.cc: In function ‘int getTransID()’:
Transaction.cc:18:34: error: ‘transID’ was not declared in this scope
int getTransID() { return transID; }
^
Transaction.cc: In function ‘TransType getTType()’:
Transaction.cc:19:34: error: ‘tType’ was not declared in this scope
TransType getTType() { return tType; }
^
Transaction.cc: In function ‘TransState getTState()’:
Transaction.cc:20:34: error: ‘tState’ was not declared in this scope
TransState getTState() { return tState; }
^
Transaction.cc: In function ‘std::__cxx11::string getDate()’:
Transaction.cc:21:34: error: ‘date’ was not declared in this scope
string getDate() { return date; }
^
Transaction.cc: In function ‘int getTAcctNum()’:
Transaction.cc:22:34: error: ‘tAcctNum’ was not declared in this scope
int getTAcctNum() { return tAcctNum; }
^
Transaction.cc: In function ‘float getTAmount()’:
Transaction.cc:23:34: error: ‘tAmount’ was not declared in this scope
float getTAmount() { return tAmount; }
^
Transaction.cc: In function ‘void setDate(std::__cxx11::string)’:
Transaction.cc:28:3: error: ‘date’ was not declared in this scope
date = d;
^
Makefile:31: recipe for target 'Transaction.o' failed
make: *** [Transaction.o] Error 1
Here is my header file
#ifndef TRANSACTION_H
#define TRANSACTION_H
#include <string>
using namespace std;
#include "defs.h"
class Transaction
{
public:
Transaction(TransType = TTERROR, TransState = TSERROR,int = 0 ,float = 0);
int getTransID();
TransType getTType();
TransState getTState();
string getDate();
int getTAcctNum();
void setDate(string);
float getAmount();
private:
static int nextTransID;
int transID;
TransType tType;
TransState tState;
string date;
int tAcctNum;
float tAmount;
};
#endif
Here is my source file
#include "Transaction.h"
#include "defs.h"
#include <string>
using namespace std;
int Transaction::nextTransID = 2001;
Transaction::Transaction(TransType t, TransState s, int acct, float amount)
{
transID = nextTransID++;
tType = t;
tState = s;
tAcctNum = acct;
tAmount = amount;
}
int getTransID() { return transID; }
TransType getTType() { return tType; }
TransState getTState() { return tState; }
string getDate() { return date; }
int getTAcctNum() { return tAcctNum; }
float getTAmount() { return tAmount; }
void setDate(string d)
{
date = d;
}
I am kinda lost on what is the issue
This:
int getTransID() { return transID; }
has nothing to do with your class, it's a global function.
You meant:
int Transaction::getTransID() { return transID; }
Also, that function (and the other getters) should be made const to signal that they don't modify the object.