из класса CConst:
| C++ | ||
|
Что такое arg ? Некое данное-член класса? Но где его объявление?
| C++ | ||
|
Попытка создать экземпляр чисто абстрактного класса? Компилятор пошлет вас «далече»…
| C++ | ||
|
Зачем параметр x , если вы просто возвращаете значение данного-члена value ?
| C++ | ||
|
Зачем в return указывать возвращаемый тип? Как аргументом return может быть toString(value), если сигнатура функции имеет вид string toString()
В классе CArg — то же самое.
Для начала исправьте ошибки в классах CConst и CArg, и добавьте включение заголовочных файлов этих классов в main.cpp
| C++ | ||
|
Строка Csum* c =new Csum(new CArg* l,new CConst* r); — new изначально возвращает указатель на вновь созданный объект, смысл в операции new CArg* ?
В других классах тоже есть к чему «придраться», например, все данные класса должны объявляться в private-секции класса.
Вывод: перепишите «с нуля», предварительно удостоверившись, что вы четко понимаете, чего именно хотите добиться.
Please use this template for reporting suspected bugs or requests for help.
Trying to compile libzmq with MinGW — the issue is fairly obvious. __except should not have gone through a code review as it’s a Microsoft specific extension.
src/thread.cpp:128: warning: ignoring #pragma warning [-Wunknown-pragmas]
#pragma warning(push)
src/thread.cpp:129: warning: ignoring #pragma warning [-Wunknown-pragmas]
#pragma warning(disable : 6320 6322)
src/thread.cpp:138: warning: ignoring #pragma warning [-Wunknown-pragmas]
#pragma warning(pop)
src/thread.cpp: In member function 'void zmq::thread_t::applyThreadName()':
src/thread.cpp:136:5: error: expected 'catch' before '__except'
__except (EXCEPTION_CONTINUE_EXECUTION) {
^~~~~~~~
src/thread.cpp:136:5: error: expected '(' before '__except'
__except (EXCEPTION_CONTINUE_EXECUTION) {
^~~~~~~~
(
src/thread.cpp:136:5: error: expected type-specifier before '__except'
__except (EXCEPTION_CONTINUE_EXECUTION) {
^~~~~~~~
src/thread.cpp:136:13: error: expected ')' before '(' token
__except (EXCEPTION_CONTINUE_EXECUTION) {
~~~~~~~~^~
)
src/thread.cpp:136:14: error: expected '{' before '(' token
__except (EXCEPTION_CONTINUE_EXECUTION) {
^
src/thread.cpp:136:44: error: expected ';' before '{' token
__except (EXCEPTION_CONTINUE_EXECUTION) {
^~
;
Can provide a Dockerfile upon request.
C++ Online Compiler
Write, Run & Share C++ code online using OneCompiler’s C++ online compiler for free. It’s one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler’s C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++ and start coding!
Read inputs from stdin
OneCompiler’s C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
About C++
C++ is a widely used middle-level programming language.
- Supports different platforms like Windows, various Linux flavours, MacOS etc
- C++ supports OOPS concepts like Inheritance, Polymorphism, Encapsulation and Abstraction.
- Case-sensitive
- C++ is a compiler based language
- C++ supports structured programming language
- C++ provides alot of inbuilt functions and also supports dynamic memory allocation.
- Like C, C++ also allows you to play with memory using Pointers.
Syntax help
Loops
1. If-Else:
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
2. Switch:
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
3. For:
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
4. While:
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
5. Do-While:
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Functions
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
How to declare a Function:
return_type function_name(parameters);
How to call a Function:
function_name (parameters)
How to define a Function:
return_type function_name(parameters) {
// code
}
Many warnings and mistakes in your code (not saying this to put you down or anything) so I will try to take it step by step.
Firstly, you should define your classes in your header (.h) files. That is, move your two derived classes (‘Staircase’ and ‘Localization’) definitions into the same header file as the base class (‘HardwareDiagnostic’) OR create seperate header files for them. The first way of doing things is easier but the second one is (sometimes) much cleaner and easier to refactor later on.
So your HardwareDiagnostic.h would be like this:
#ifndef HARDWAREDIAGNOSTIC_H_
#define HARDWAREDIAGNOSTIC_H_
#include <iostream>
class HardwareDiagnostic
{
public:
virtual bool checkPort() = 0;
virtual void startDiagnostic() = 0;
virtual int publishAlgoDiagnosticInfo() = 0;
virtual void clearErrorStatus() {
std::cout << "Algorithm Diagnostics" << std::endl;
}
virtual ~HardwareDiagnostic() {
std::cout << "calling BASE virtual destructor" << std::endl;
}
};
class Localization : public HardwareDiagnostic
{
public:
Localization() {
std::cout << "calling Localization constructor";
}
bool checkPort() {
std::cout << "checkport :Localization";
}
void startDiagnostic() {
std::cout << "start Diagnostic:Localization";
}
int publishAlgoDiagnosticInfo() {
std::cout << "publish Diagnostic:Localization";
}
void clearErrorStatus() {
std::cout << "Localization:publish Diagnostic";
}
~Localization () {
std::cout << "calling Localization destructor ";
}
};
class Staircase : public HardwareDiagnostic
{
public:
Staircase () {
std::cout << "Staircase constructor";
}
bool checkPort() {
std::cout << "Staircase";
}
void startDiagnostic() {
std::cout << "StairCase:start Diagnostic";
}
int publishAlgoDiagnosticInfo() {
std::cout << "StairCase:publish Diagnostic";
}
void clearErrorStatus() {
std::cout << "staircase:publish Diagnostic";
}
~Staircase() {
std::cout << "calling Staircase destructor";
}
};
#endif /* HARDWAREDIAGNOSTIC_H_ */
And your HardwareDiagnostic.cpp would just include his header file and be empty after.
Now your main would run the line of code just fine:
#include "HardwareDiagnostic.h"
#include <memory>
int main()
{
std::unique_ptr<HardwareDiagnostic> algodiagnostic (new Staircase());
return 0;
}
There are, though, a series of problems with your code. You’re trying to derive from a base class, that, by the looks of your initial code, hasn’t an implementation (it should’ve been present in the cpp file, but it isn’t). So I guess, what you want to do, is to declare the first functions as pure virtual functions. This will prevent you from creating an instance of the base class, by the way. Also return from functions when you have different return values than void (see the warnings from compiler) and stop using ‘using namesapce std’ as it’s a bad practice, that pollutes the global namespace. About the smart pointers, you could just use: auto algodiagnostic = std::make_unique<Staircase>(); since it’s
(nearly) the same things and it’s shorter / cleaner.
In the end, this is how I would code these classes (Note: I would separate the derived classes in separate headers. Now, for pure example purposes, I just cram them into a single header file):
HardwareDiagnostic.h:
#ifndef HARDWAREDIAGNOSTIC_H_
#define HARDWAREDIAGNOSTIC_H_
struct HardwareDiagnostic
{
HardwareDiagnostic() = default;
virtual ~HardwareDiagnostic();
virtual bool checkPort() const = 0;
virtual void startDiagnostic() const = 0;
virtual int publishAlgoDiagnosticInfo() const = 0;
virtual void clearErrorStatus() const;
};
struct Localization : public HardwareDiagnostic
{
Localization();
~Localization();
virtual bool checkPort() const override;
virtual void startDiagnostic() const override;
virtual int publishAlgoDiagnosticInfo() const override;
virtual void clearErrorStatus() const override;
};
struct Staircase : public HardwareDiagnostic
{
Staircase();
~Staircase();
virtual bool checkPort() const override;
virtual void startDiagnostic() const override;
virtual int publishAlgoDiagnosticInfo() const override;
virtual void clearErrorStatus() const override;
};
#endif /* HARDWAREDIAGNOSTIC_H_ */
HardwareDiagnostic.cpp:
#include "HardwareDiagnostic.h"
#include <iostream>
void HardwareDiagnostic::clearErrorStatus() const
{
std::cout << "Algorithm Diagnostics" << std::endl;
}
HardwareDiagnostic::~HardwareDiagnostic()
{
std::cout << "calling BASE virtual destructor" << std::endl;
}
Localization::Localization()
{
std::cout << "calling Localization constructor" << std::endl;
}
bool Localization::checkPort() const
{
std::cout << "checkport :Localization" << std::endl;
return true; // just a dummy return (to be implemented)
}
void Localization::startDiagnostic() const
{
std::cout << "start Diagnostic:Localization";
}
int Localization::publishAlgoDiagnosticInfo() const
{
std::cout << "publish Diagnostic:Localization" << std::endl;
return 0; // just a dummy return (to be implemented)
}
void Localization::clearErrorStatus() const
{
std::cout << "Localization:publish Diagnostic" << std::endl;
}
Localization::~Localization()
{
std::cout << "calling Localization destructor " << std::endl;
}
Staircase::Staircase()
{
std::cout << "Staircase constructor" << std::endl;
}
bool Staircase::checkPort() const
{
std::cout << "Staircase" << std::endl;
return true; // just a dummy return (to be implemented)
}
void Staircase::startDiagnostic() const
{
std::cout << "StairCase:start Diagnostic" << std::endl;
}
int Staircase::publishAlgoDiagnosticInfo() const
{
std::cout << "StairCase:publish Diagnostic" << std::endl;
return 0; // just a dummy return (to be implemented)
}
void Staircase::clearErrorStatus() const
{
std::cout << "staircase:publish Diagnostic" << std::endl;
}
Staircase::~Staircase()
{
std::cout << "calling Staircase destructor" << std::endl;
}
main.cpp:
#include "HardwareDiagnostic.h"
#include <memory>
int main()
{
auto algodiagnostic = std::make_unique<Staircase>();
return 0;
}
