Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I'm currently making my first project in codeblocks, but when i generate a new class it pops up a bunch of errors.
Code:
#ifndef SERVICIO_H
#define SERVICIO_H
#include <iostream>
#include <string>
using namespace std;
class Servicio
{
public:
Servicio();
virtual ~Servicio();
int codigo Get[10]() { return [10]; }
void Set[10](int codigo val) { [10] = val; }
string nombre Get[10]() { return [10]; }
void Set[10](string nombre val) { [10] = val; }
float precio Get[10]() { return [10]; }
void Set[10](float precio val) { [10] = val; }
float comision Get[10]() { return [10]; }
void Set[10](float comision val) { [10] = val; }
protected:
private:
int codigo [10];
string nombre [10];
float precio [10];
float comision [10];
}
#endif // SERVICIO_H
And the error log:
|12|error: expected ';' at end of member declaration|
|12|error: 'Get' does not name a type|
|13|error: expected ',' or '...' before 'val'|
|13|error: declaration of 'Set' as array of functions|
|13|error: expected ';' at end of member declaration|
|14|error: expected ';' at end of member declaration|
|14|error: 'Get' does not name a type|
|15|error: expected ',' or '...' before 'val'|
You need a ; after the closing bracket of the class.
If you can use C++11, then consider using std::array. See this for details.
#include <array>
#include <iostream>
class Servicio
{
public:
Servicio() { }
virtual ~Servicio() { }
We don't want to return by reference since you only want to get the value.
std::array<int, 10> get_codigo() const {
return codigo;
}
Here you can consider doing something with value before assigning it to codigo.
void set_codigo(const std::array<int, 10>& value) {
codigo = value;
}
protected:
private:
std::array<int, 10> codigo;
std::array<std::string, 10> nombre;
std::array<float, 10> precio;
std::array<float, 10> comision;
};
Either way this style of coding is cumbersome and probably not the correct approach.
What? This code is nothing like C++. You really need to read a book before you start coding. C++ is very different from whatever language you knew before. It's not just syntactically different, the concepts are different. You are not going to be able to code C++ just by using what you know already, you are going to have to do some studying.
I guess you're not likely to take the advice above, so here's start, it is at least legal code (but not good code though).
class Servicio
{
public:
Servicio();
int* GetCodigo() { return codigo; }
...
private:
int codigo [10];
};
Related
In following code, i get an error on line 33. Why? What is the correct syntax?
Surely I made some stupid mistake ... unfortunately I'm trying to better understand the vectors.
#include <iostream>
#include <vector>
class firstClass
{
public:
firstClass(int x, int y):sum(x+y)
{
}
void getSum()
{
std::cout << sum << std::endl;
}
private:
int sum;
};
class secondClass
{
public:
secondClass(int dim)
{
obj = new std::vector<firstClass>(dim,firstClass{3,5});
}
private:
std::vector<firstClass>*obj;
};
int main()
{
secondClass*obj2;
obj2 = new secondClass(4);
obj2->(*obj)[0].getSum(); //HERE!
return 0;
}
Error:
error: expected unqualified-id before '(' token
error: 'obj' was not declared in this scope
The correct syntax for accessing the data member should be:
(*(obj2->obj))[0].getSum();
Note that secondClass::obj is private data member, so you can't access it in main().
For code sample you showed, you don't need to use raw pointer and new at all.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Is there a way to allocate a variable within the scope of a parameter list? By using new we can do the following :
Class A{ /*... snip ...*/ };
void myFunc(A* a){ }
int main(...){
myFunc(new A());
return 0;
}
This will create a new A. What if the signature of myFunc was
void myFunc(A a);
instead. Is there a syntax to create local instance inside the myFunc() parameter list? I'm looking for something like
myFunc(A());
or
myFunc(A a());
Another use would be for something like :
A a(123);
if(a == A(123)){ }
The net effect is to save one line, but it also creates a scope within the parameters list which makes me wonder if it is allowed at all.
If you just want to create a variable to pass to the function you can use a aggregate initialization / list initialization
#include <iostream>
#include <cmath>
using namespace std;
class A{ /*... snip ...*/ };
void myFunc(A a){ }
int main(){
myFunc(A{});
return 0;
}
Live Example
You can also use this with classes that have constructors that take multiple parameters
#include <iostream>
#include <cmath>
using namespace std;
class A
{
private:
int foo;
int bar;
double foobar;
public:
A(int a, int b, double c) : foo(a), bar(b), foobar(c) {}
};
void myFunc(A a){ }
int main(){
myFunc(A{1,2,3.0});
return 0;
}
Live Example
C++ supports this with the myFunc(A()); syntax you posed in your question.
#include <stdio.h>
char lazybuff[500];
class Point
{
public:
Point (double x, double y) : m_x(x), m_y(y) { }
char * ToString (void) { sprintf (lazybuff, "%f, %f", m_x, m_y); return lazybuff; }
private:
double m_x, m_y;
};
void print_point (Point print_me)
{
printf ("%s\n", print_me.ToString());
}
int main (void)
{
print_point (Point(5, 3));
return 0;
}
Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 7 years ago.
Improve this question
I got a problem when I want to pass an enum value to a default construtor. My enums are defined like this:
typedef enum
{
DOUBLOON,
VICTORYPOINT
} ENUMchipType;
They are stored in a separate .h file.
But when i try to do this:
chips m_doubloon(DOUBLOON);
I get the following error:
error: C2061: syntax error : identifier 'DOUBLOON'
The code for the default constructor is:
chips::chips(
ENUMchipType chipType = DOUBLOON,
int amountValue1 = 0,
int amountValue5 = 0,
QObject *parent = 0) :
m_chipType(chipType),
m_chipCountValue1(amountValue1),
m_chipCountValue5(amountValue5),
QObject(parent) {}
Anyone an idea what is wrong with this piece of code? Thanks in advance!
Edit: I already tried putting the enum is a class als a public member and derive the chips class from it, but without any succes.
EDIT 2: This piece of code reproduces the error in Visual Studio 2013
#include <string>
using namespace std;
//enums.h
typedef enum
{
DOUBLOON,
VICTORYPOINT
} ENUMchipType;
typedef enum
{
PLAYER1,
PLAYER2,
PLAYER3,
PLAYER4,
PLAYER5
} ENUMplayer;
// In chips.h
class chips
{
private:
int m_chipCountValue5;
int m_chipCountValue1;
ENUMchipType m_chipType;
public:
explicit chips(
ENUMchipType chipType = ENUMchipType::DOUBLOON,
int amountValue1 = 0,
int amountValue5 = 0);
ENUMchipType getChipType() const { return m_chipType; }
};
// Chips.cpp
chips::chips(ENUMchipType chipType, int amountValue1, int amountValue5) :
m_chipType(chipType),
m_chipCountValue1(amountValue1),
m_chipCountValue5(amountValue5) {}
// PLayer.h
class player
{
private:
ENUMplayer m_ID;
string m_name;
public:
chips m_doubloon(DOUBLOON);
chips m_victoryPoints(VICTORYPOINT);
explicit player(ENUMplayer ID = PLAYER1, string name = "");
void setName(string name = "") { m_name = name; }
void setID(ENUMplayer ID) { m_ID = ID; }
string getName() const { return m_name; }
ENUMplayer getID() const { return m_ID; }
};
//player.cpp
player::player(ENUMplayer ID, string name) :
m_ID(ID),
m_name(name) {}
int main() {
return 0;
}
Now you've finally posted enough code, we see that this
chips m_doubloon(DOUBLOON);
is actually a class member declaration. Class members can't be initialised with (), only with = or {}. Assuming your compiler supports in-class initialisation (introduced in C++11), you should be fine with
chips m_doubloon{DOUBLOON};
^ ^
Alternatively, you could initialise the members in the constructor's initialiser list rather than in their declarations.
In class player, you should replace
chips m_doubloon(DOUBLOON);
chips m_victoryPoints(VICTORYPOINT);
by
chips m_doubloon{DOUBLOON};
chips m_victoryPoints{VICTORYPOINT};
You need to pass DOUBLOON as ENUMchipType::DOUBLOON
I'm supposed to create a template class for an assignment, but I'm getting lots of different errors that I don't really understand, can someone please help me? I have attached the cp and header files that I wrote. I know this is probably very simple but I'm new to this, thank you!
#ifndef __Template_example__Initialisedchecker__
#define __Template_example__Initialisedchecker__
#include <stdio.h>
template <class data>
class Initialisedchecker
{
private:
data item;
bool definedOrN;
public:
Initialisedchecker()
{
definedOrN = false;
}
void setItem(const data&)
{
std::cin >> item;
definedOrN = true;
}
void displayItem()
{
if (definedOrN)
{
std::cout << item;
}
else
{
std::cout << "error, your item is undefined";
}
}
};
#endif
And this is the main:
#include <iostream>
#include "Initialisedchecker.h"
using namespace std;
int main()
{
item <int> x;
displayItem();
x = 5;
displayItem();
}
Sorry, I forgot to add the errors I'm getting, the header file doesn't give any errors, but in the main, it says:
Use of undeclared identifier 'display item' ,
Use of undeclared identifier 'item' ,
Use of undeclared identifier 'x' ,
Expected a '(' for function-style cast or type construction
The class template is called Initialisedchecker, not item. And you need to call the member function on the object. You need:
int main()
{
Initialisedchecker <int> x;
x.displayItem();
// this is strange: x = 5;
// maybe use:
// x.setItem( 5 );
x.displayItem();
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Header:
#include <unordered_map>
#include "O.h"
#include "P.h"
using namespace std;
class O{
public:
O();
unordered_map<int,P>* X();
unordered_map<int,P>* Y();
private:
unordered_map<int,P>* b;
unordered_map<int,P>* a;
};
source:
#include "O.h"
#include "P.h"
#include <unordered_map>
using namespace std;
O::O(){
a= new unordered_map<int,P>();
b= new unordered_map<int,P>();
}
unordered_map<int,P>* O::X(){
return b;
}
unordered_map<int,P>* O::Y(){
return a;
}
Error is:
1>O.cpp(76): error : return value type does not match the function
type 1> return b;
1>O.cpp(80): error : return value type does not match the function
type 1> return a;
I'm going crazy trying to debug this....
EDIT: Intel Compiler v13
The code you have posted is valid C++, so the issue must lie elsewhere in your code. I would check the included headers. Here is an example with a valid declaration of P:
#include <unordered_map>
using namespace std;
class P{
public:
int a = 3;
};
class O{
public:
O();
unordered_map<int,P>* X();
unordered_map<int,P>* Y();
private:
unordered_map<int,P>* b;
unordered_map<int,P>* a;
};
O::O(){
a= new unordered_map<int,P>();
b= new unordered_map<int,P>();
}
unordered_map<int,P>* O::X(){
return b;
}
unordered_map<int,P>* O::Y(){
return a;
}
int main(){
O o;
auto map = o.X();
return 0;
}
ideone: http://ideone.com/Y4ydzj