I'm trying to create a method in a C++ class that can be called without creating an instance of the class (like a static method in Java), but I keep running into this error: error: expected unqualified-id before ‘.’ token
Here's the .cpp file I'm trying to compile:
using namespace std;
#include <iostream>
class Method {
public:
void printStuff(void) {
cout << "hahaha!";
}
};
int main(void){
Method.printStuff(); // this doesn't work as expected!
return 0;
}
In C++ it's
Method::printStuff();
and you have to declare the method as static.
class Method{
public:
static void printStuff(void){
cout << "hahaha!";
}
};
:: is called the scope resolution operator. You can call the method with . if it's on a class instance, but the instance is not required (it being static and all...).
Related
I've been running across this snippet of code and after execution I found out that everything compiles and executes fine (the int code member of the derived class is set to 65). However I was wondering how would one be able to access the char code member of the derived class?
#include <iostream>
using namespace std;
class base {
public:
base() : code('B') { }
char code;
};
class derived : public base
{
public:
int code;
};
int main(void)
{
derived d;
d.code = 65;
std::cout << d.code;
};
By specifying the correct scope for the base member variable using a qualified name lookup, as follows:
d.base::code = 'x'
std::cout << d.base::code << '\n';
See this section on qualified name lookups for more details.
just started to learn c++.I'm trying new things in c++ on thing i wanted to try is to access a class from another class and change its instances and print its instance on screen.
I would like to know 2 things 1)whats wrong with my code 2)where should i declare class declarations (in main file or class definition file?)
here is the error log -
'object::carrier' uses undefined class 'sub'
'cout': is not a member of 'std'
'cout': undeclared identifier
this is what i came up with-
source.h
#include <iostream>
#include <vector>
#include "stuff.h"
int main()
{
object spoon(3);
spoon.get();
}
stuff.cpp
#pragma once
#include <vector>
class object;
class sub;
class object
{
private:
std::vector <sub> thing;
public:
object(int n);
void get() const;
};
class sub
{
private:
int num;
public:
void set_num(int n);
};
stuff.cpp
#include <vector>
#include "stuff.h"
// methods for object
object::object(int n)
{
sub carrier;
carrier.set_num(n);
}
void object::get() const
{
std::cout << carrier.num;
}
// methods for sub
void sub::set_num(int temp_num)
{
num = temp_num;
}
thanks
In your object class, specifically object::get definitions, you use the variable carrier without it being in scope.
When you declare the variable sub carrier in your constructor, it is only accessible in the same scope, that is, inside the constructor. Once your program leaves the scope, the variable carrier is deallocated (deleted).
You must add the variable sub carrier as a member to your class like so:
class object
{
private:
sub carrier
// other stuff
}
Edit:
I so you edited your question.
You must either replace cout with std::cout because cout is part of the c++ standard library. Alternatively, a less verbose option would be to add using namespace std; at the top of every .cpp file. This basically tells the compiler that you can use the namespace std without explicitly saying it. But don't do it for .h files. It's not a good idea.
This question already has answers here:
Why can I use auto on a private type?
(5 answers)
Closed 2 years ago.
Good morning everybody,
I'm trying to write a member function of a class OUTER_CLASS which returns a unique_ptr to a private class INNER_CLASS defined within the OUTER_CLASS itself. My intent is to write a factory method which returns a reference to an Interface Class which allows to interact with the Implementation Class hided from client. Apart from the design choice, I have a compilation issue: given the following snippet of code, which is a very simplified version of my architecture
#include <iostream>
#include <string>
#include <vector>
#include <memory>
using namespace std;
class OuterClass
{
class InnerClass
{
friend class OuterClass;
public:
void myPrint() { cout << "Inner Class " << a << endl;}
InnerClass(int inA) : a(inA) {};
private:
int a;
};
public:
std::unique_ptr<InnerClass> CreateInnerClass()
{
std::unique_ptr<InnerClass> innerObj;
innerObj.reset(new InnerClass(1));
return innerObj;
}
};
int main()
{
OuterClass obj;
std::unique_ptr<OuterClass::InnerClass> innerObj = obj.CreateInnerClass();
innerObj->myPrint();
}
I get following error:
main.cpp: In function 'int main()':
main.cpp:43:10: error: 'class OuterClass::InnerClass' is private within this context
43 | std::unique_ptr<OuterClass::InnerClass> innerObj = obj.CreateInnerClass();
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:20:11: note: declared private here
20 | class InnerClass
| ^~~~~~~~~~
if instead i use AUTO type deduction, writing
int main()
{
OuterClass obj;
auto innerObj = obj.CreateInnerClass();
innerObj->myPrint();
}
everything is fine...
How is it possible? What am i doing wrong?
Thanks in advance for any answer
Accessibility applies on name, not the type being referred to. So you just can't use the name OuterClass::InnerClass directly in main(), but you can still use the type like calling member functions on the returned object whose type is OuterClass::InnerClass like
obj.CreateInnerClass()->myPrint();
Given auto innerObj = obj.CreateInnerClass(); the name OuterClass::InnerClass is not used directly then it doesn't violate the accessibility check. For the same reason you can use decltype too, even it seems redundant. E.g.
decltype(obj.CreateInnerClass()) innerObj = obj.CreateInnerClass();
I'm trying to make the following implementation work:
Header of base class:
#ifndef BASE_HH
#define BASE_HH
namespace base {
class Agent
{
public:
int loop () {return loopImpl();}
protected:
virtual int loopImpl() =0;
}
#endif
Header or derived class:
#ifndef DERIVED_HH
#define DERIVED_HH
#include <base/agent.hh>
namespace derived {
class Agent : public base::Agent
{
protected:
virtual int loopImpl();
}
#endif
Source file of derived class:
#include <derived/agent.hh>
int Agent::loopImpl()
{
return 0;
}
Now, when I compile this in the terminal and test it, it works. But when I try compiling an S-function in Matlab that creates an Agent object, i get the following error:
Error using mex
/path/sfunction.cpp In function ‘void mdlStart(SimStruct*)’:
/path/sfunction.cpp:51:56: error: invalid new-expression of abstract class type ‘derived::Agent’
derived::Agent *agent = new derived::Agent ();
/derived_path/agent.hh:28:11: note: because the following virtual functions are pure within ‘derived::Agent’:
class Agent: public base::Agent
In file included from /derived_path/agent.hh:22:0, from /path/sfunction.cpp:13:
/base_path/agent.hh:54:21: note: virtual int base::Agent::loopImpl() virtual int loopImpl () =0;
So it seems the g++4.9 compiler is unable to find the derived member function... Could anybody give me some hints on why this is so and what to do about it? As mentioned above, when I compile a similar file creating an object of the same derived class, it works.
Thank you for your time.
class Example{
public:
friend void Clone::f(Example);
Example(){
x = 10;
}
private:
int x;
};
class Clone{
public:
void f(Example ex){
std::cout << ex.x;
}
};
When I write f as a normal function, the program compiles successful. However, when I write f as a class member, this error occurs.
Screenshot:
The error you're seeing is not a root-cause compilation error. It is an artifact of a different problem. You're friending to a member function of a class the compiler has no earthly clue even exists yet,much less exists with that specific member.
A friend declaration of a non-member function has the advantage where it also acts as a prototype declaration. Such is not the case for a member function. The compiler must know that (a) the class exists, and (b) the member exists.
Compiling your original code (I use clang++ v3.6), the following errors are actually reported:
main.cpp:6:17: Use of undeclared identifier 'Clone'
main.cpp:17:25: 'x' is a private member of 'Example'
The former is a direct cause of the latter. But doing this instead:
#include <iostream>
#include <string>
class Example;
class Clone
{
public:
void f(Example);
};
class Example
{
public:
friend void Clone::f(Example);
Example()
{
x = 10;
}
private:
int x;
};
void Clone::f(Example ex)
{
std::cout << ex.x;
};
int main()
{
Clone c;
Example e;
c.f(e);
}
Output
10
This does the following:
Forward declares Example
Declares Clone, but does not implement Clone::f (yet)
Declares Example, thereby making x known to the compiler.
Friends Clone::f to Example
Implements Clone::f
At each stage we provide what the compiler needs to continue on.
Best of luck.