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.
Related
I am so confused right now...
Here is the code that I will be talking about:
main.cpp:
#include "CustomVector.h"
#include <iostream>
int main() {
CustomVector<int, 15> hello{};
std::cout << hello.size() << '\n';
return 0;
}
CustomVector.h:
#pragma once
template<typename T, int S>
class CustomVector {
private:
T arr[S];
int size;
public:
CustomVector() : arr{}, size{ S } {}
// Methods
int size() const {
return size;
}
};
As you can see I am trying to use the size() method that I have in my class definition, which there shouldn't be a problem with, right..? But when I try to use it in main.cpp and try to print it out to the console, it is giving me the compiler error which my question title has. Why could this be, this has never happened to me before. I would think there is some sort of "redefinition" somehow, but how could that be, there couldn't be a redefinition if it is in my own user-defined class?
I'm trying to create an array of objects within another object and decide the magnitude of the array.
Why I get an error when I try to assign "obj2T" to "obj2"?
Pastebin code link: https://pastebin.com/kujujP5N
What is the correct syntax for creating an array of objects within another object and decide the magnitude of the array?
#include <iostream>
using namespace std;
class classe2
{
public:
classe2();
protected:
private:
};
class classe1
{
public:
classe1(int value);
void setClasse()
{
classe2 obj2T[grandezza];
obj2=obj2T;
}
protected:
private:
const int grandezza;
classe2 obj2[];
};
int main()
{
cout << "Hello World!" << endl;
return 0;
}
Error:
C:\cppProjects\project\main.cpp||In member function 'void classe1::setClasse()'
C:\cppProjects\project\main.cpp|22|error: incompatible types in assignment of 'classe2 [((classe1*)this)->classe1::grandezza]' to 'classe2 [0]'
The correct syntax is
#include <vector>
...
class classe1
{
public:
classe1(int value) :
obj2 (value)
{
}
private:
std::vector<classe2> obj2;
};
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();
}
I'm new to C++ and get a beginner's mistake:
myclass.cpp: In function ‘int main()’:
myclass.cpp: 14:16: error: ‘func’ was not declared in this scope
This is the code:
#include <iostream>
using namespace std;
class MyClass{
public:
int func(int);
};
int MyClass::func(int a){
return a*2;
}
int main(){
cout << func(3);
}
I hope you can help me.
int main(){
cout << func(3);
}
func is not a global function; it is a member function of the class. You need an instance of the class to access it.
For example:
int main()
{
MyClass obj;
std::cout<< obj.func(3);
}
func is a member function, so it must be invoked through an object. For example:
int main()
{
MyClass obj;
std::cout << obj.func(3); // 6
}
In your example, you treated it as a free function, so the compiler looked for a function with that name. Since it could not find it, it issued a compiler error.
func is a member function of MyClass. To call it, you need an object of MyClass type to invoke it on:
int main(){
MyClass m; // Create a MyClass object
cout << m.func(3);
}
Alternatively, you could make func a static member function, which means that it is not associated with any particular instance of the class. However, you would still need to qualify its name as belonging to the MyClass class:
class MyClass{
public:
static int func(int);
};
int MyClass::func(int a){
return a*2;
}
int main(){
cout << MyClass::func(3);
}
#include <iostream>
using namespace std;
class B
{
public:
int getMsg(int i)
{
return i + 1;
}
};
class A
{
B b;
public:
void run()
{
taunt(b.getMsg);
}
void taunt(int (*msg)(int))
{
cout << (*msg)(1) << endl;
}
};
int main()
{
A a;
a.run();
}
The above code has a class B inside a class A, and class A has a method taunt that takes a function as an argument. class B's getMsg is passed into taunt...The above code generated the following error message: "error: no matching function for call to 'A::taunt()'"
What's causing the error message in the above code? Am I missing something?
Update:
#include <iostream>
using namespace std;
class B
{
public:
int getMsg(int i)
{
return i + 1;
}
};
class A
{
B b;
public:
void run()
{
taunt(b.getMsg);
}
void taunt(int (B::*msg)(int))
{
cout << (*msg)(1) << endl;
}
};
int main()
{
A a;
a.run();
}
t.cpp: In member function 'void A::run()':
Line 19: error: no matching function for call to 'A::taunt()'
compilation terminated due to -Wfatal-errors.
I'm still getting the same error after changing (*msg)(int) to (B::*msg)(int)
b.getMsg is not the correct way to form a pointer to member, you need &B::getMsg.
(*msg)(1) is not the correct way to call a function through a pointer to member you need to specify an object to call the function on, e.g. (using a temporary) (B().*msg)(1).
The right way to do such things in OOP is to use interfaces so all you need to do is to define an interface and implement it in B class after that pass the pointer of instance which implements this interface to your method in class A.
class IB{
public:
virtual void doSomething()=0;
};
class B: public IB{
public:
virtual void doSomething(){...}
};
class A{
public:
void doSomethingWithB(IB* b){b->doSomething();}
};
This works in VS 2010. The output is the same on all lines:
#include <iostream>
#include <memory>
#include <functional>
using namespace std;
using namespace std::placeholders;
class A
{
public:
int foo(int a, float b)
{
return int(a*b);
}
};
int main(int argc, char* argv[])
{
A temp;
int x = 5;
float y = 3.5;
auto a = std::mem_fn(&A::foo);
cout << a(&temp, x, y) << endl;
auto b = std::bind(a, &temp, x, y);
cout << b() << endl;
auto c = std::bind(std::mem_fn(&A::foo), &temp, _1, y);
cout << c(5) << endl;
}
Basically, you use std::mem_fn to get your callable object for the member function, and then std::bind if you want to bind additional parameters, including the object pointer itself. I'm pretty sure there's a way to use std::ref to encapsulate a reference to the object too if you'd prefer that. I also included the _1 forwarding marker just for another way to specify some parameters in the bind, but not others. You could even specify everything BUT the class instance if you wanted the same parameters to everything but have it work on different objects. Up to you.
If you'd rather use boost::bind it recognizes member functions and you can just put it all on one line a bit to be a bit shorter: auto e = boost::bind(&A::foo, &temp, x, y) but obviously it's not much more to use completely std C++11 calls either.