no default constructor exists for class (c++ classes) [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
#include<iostream>
#include<string>
using namespace std;
#ifndef TicTac_H
#define TicTac_H
class TicTac
{
public:
TicTac(int ,int);
void setpos(int);
void getpos(int);
void setpos2(int);
void getpos2(int);
bool takepos();
void setar(int&, int&);
void setarr();
void all(int,int);
void print();
int test();
private:
int p1;
int p2;
string tic[3][3] ;
string x;
string o;
int t1;
int t2;
bool ok;
};
#endif
**The compiler shown this message :
no default constructor exists for class "TicTac"
'TicTac' : no appropriate default constructor available
can anybody help me to fix this problem **

The error is surely not in that code, but in the code that includes that header and attempts to create an object of type TicTac without providing the two arguments that the constructor takes (two int). Other than that, the include guards should cover all the file (including the #include<...>) and you should never have a using directive (using namespace X) in a header.

Related

c++ - struct isn't recognized when using in a header file [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I have a file named: FeedbackCircuitVariables.h with the following inside:
#pragma once
struct FeedbackCircuitVariables {
// Unknown values (the ones which will be calculated).
float m_Ic, m_Ib;
float m_Vce;
// Known values (the ones asked to user through keyboard).
float m_Rc, m_Rb, m_Re;
float m_Vbe, m_Vcc;
};
And a class in FeedbackCircuit.h:
#pragma once
#include "FeedbackCircuitVariables.h"
class FeedbackCircuit {
private:
FeedbackCircuitVariables *m_pVariables;
public:
FeedbackCircuit(const FeedbackCircuitVariables *variables);
};
And this is the definition of that class:
#include "FeeedbackCircuit.h"
FeedbackCircuit(const FeedbackCircuitVariables *variables) {
}
But inside the header of the class the compiler says that FeedbackCircuitVariables isn't a type name.
What am I doing wrong?
Your constructor syntax is wrong.
Change:
FeedbackCircuit(const FeedbackCircuitVariables *variables)
To:
FeedbackCircuit::FeedbackCircuit(const FeedbackCircuitVariables *variables)
// ^^^^^^^^^^^^^^^^^ This is the scope of the thing you're defining.
Your code should build then.
You have to set the scope of FeedbackCircuit(const FeedbackCircuitVariables *variables) in FeedbackCircuit.cpp:
FeedbackCircuit::FeedbackCircuit(const FeedbackCircuitVariables *variables) {
}
Add FeedbackCircuit:: to tell the editor and compiler where the function is located.

Can't call functions from another header file when not in main [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
So I have test.h which contains:
#ifndef TEST_H_
#define TEST_H_
class test {
public:
int value;
};
#endif /* TEST_H_ */
and my main.cpp:
#include "test.h"
class Magic {
test x;
x.value = 2; // Syntax error
};
int main () {
test y;
y.value = 2; // Works fine
return 0;
}
Why is this happening?
Assigning values like that is not valid syntax in a class definition in c++. The error has nothing to do with headers or whatever. Try putting everything in a single file and you will see the same behavior.
If you want to have default initialization of x.value to 2 for each instance of Magic define this in the constructor of Magic:
class Magic {
test x;
Magic() {
x.value = 2;
}
};

Type '' could not be resolved, template type [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am a bit rusty with programming and couldn't find a solution on the web.
I am creating a template class, and when using it I seem to be getting the error:
Type 'temp' could not be resolved
I have to tried to clean and rebuild, but it didn't help. Any advice is appreciated.
/*
* avlTREE.h
*
* Created on: May 1, 2016
* Author:
*/
#ifndef AVLTREE_H_
#define AVLTREE_H_
#include <cassert>
#include <functional>
class avlTreeException{};
class avlTreeExceptionInvalidInput : public avlTreeException{};
template <class temp>
class avlTREE{
public:
temp* ptr;
}
#endif /* AVLTREE_H_ */
Apart from the missing semicolon, there is nothing inherently wrong with your template class.
template <class temp>
class avlTREE{
public:
temp* ptr;
};
int main(){
avlTREE<int> foo;
}

C++ compiler thinks my constructor definition is a method [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I'm getting the following errors:
C:\Users\James\cavewhere\dewalls\src\unit.cpp:6: error: C2511: 'dewalls::Unit::Unit(QString,dewalls::UnitType *)' : overloaded member function not found in 'dewalls::Unit'
C:\Users\James\cavewhere\dewalls\src\unit.cpp:9: error: C2550: 'dewalls::Unit::{ctor}' : constructor initializer lists are only allowed on constructor definitions
unit.h:
#ifndef UNIT_H
#define UNIT_H
class UnitType;
#include <QString>
namespace dewalls {
class Unit
{
public:
Unit(QString name, UnitType *type);
private:
QString _name;
UnitType *_type;
};
}
#endif // UNIT_H
unit.cpp:
#include "unit.h"
#include "unittype.h"
namespace dewalls {
Unit::Unit(QString name, UnitType *type) :
_name(name),
_type(type)
{
}
}
What for the love of god am I doing wrong?
Looks like UnitType should be in your namespace too
namespace dewalls {
class UnitType;
}

Return class type not recognized in c++ [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have written a class:
CVerifObj.hpp:
#pragma once
#include <vector>
class VerifObj
{
private:
cv::Mat m_image;
PointVector m_plateContour;
std::string m_imageName;
public:
VerifObj(const fs::path& imgNameIn);
~VerifObj();
cv::Mat getImage() const;
std::string getImageName() const;
PointVector getPlateContour() const;
};
typedef std::vector< VerifObj > VerifObjVector;
That has implementation and that is used as a type of another function in another class that includes its header:
MyCls.hpp:
#pragma once
#include "CVerifObj.hpp"
class MyCls
{
public:
MyCls();
~MyCls();
static VerifObjVector foo(); // error is here
};
The problem I get is that it is not recognized:
/home/sop/proj/CMyCls.hpp:52:2: error: ‘VerifObjVector’ does not name a type
I have added it in the CMake file too. Why is this happening?
You haven't included the std::vector definition:
#include <vector>
You're probably including MyCls.hpp in CVerifObj.hpp, directly or indirectly, which leads to a circular include. This can cause issues (undefined types).
Remove circular includes by using forward declarations.