« first day (17 days earlier)      last day (2840 days later) » 

12:19 AM
hey guys. I'm new to c++. Anyone know why my main method in a separate cpp file won't recognize the class I wrote in another cpp file?
I made a class called fraction.cpp and in the client.cpp file, the main method instantiates it as fraction f1; but XCode says it doesn't recognize this type
 
user406009
@btrballin Do you have a header?
 
user406009
That declares the class?
 
For this assignment they said don't make a separate header
`class fraction{
public:
void set(int nume, int denom);
fraction addedTo(fraction f2);
fraction subtract(fraction f2);
fraction multipliedBy(fraction f2);
fraction dividedBy(fraction f2);
bool isEqualTo(fraction f2);
void print();
fraction();
private:
int nume;
int denom;
};`
 
> >assignment
ROLL THE SIRENS
 
Don't paste code here please.
 
12:21 AM
sorry :E
 
user406009
@набиячлэвэли There is nothing wrong with people asking about homework.
 
So yeah that's the header and below that are all the methods. When I instantiate the class it won't recognize that I made that class
 
user406009
@btrballin Any reason why?
 
he meant to roll beached sirens back to the sea before they die
 
user406009
@btrballin You should upload all your code to gist.github.com so we get the complete picture.
 
and then post to Stack Overflow
no that was passive-agressive I'm sorry
 
this is a very simple problem that I don't feel requires me to create a SO question for
 
The C++ room doesn't show up in the move messages menu.
Oh well...
 
user406009
@btrballin If they don't want you to make a seperate header (because your teachers are idiots), then simply copy your class declaration to your main.cpp file.
 
12:26 AM
@btrballin How do you know whether the problem is simple if you don't know the solution?
 
ping
 
No they want us to do that later on. just not for this assignment
 
25 messages moved from Lounge<C++>
 
user406009
@btrballin Yeah, just manually copy and paste the lines then.
 
user406009
That's all a header really is anyways.
 
user406009
12:28 AM
Just an automated way of copying lines.
 
@Lalaland did u look at the 2 files I posted the URL for?
 
user406009
Yes.
 
user406009
Just copy and paste the "class fraction" junk to the main.cpp file.
 
So is it not possible to have the main.cpp contents in a separate file like that?
 
user406009
Ok, so let's clarify things. There are two things in your fraction.cpp file.
 
user406009
12:30 AM
You have the declaration of fraction and the implementation of fraction.
 
It works when I just move the main method into the fraction.cpp file, but I'm wondering if it is possible to have it work when both files are there
 
user406009
The declaration has to be copied in both main.cpp and fraction.cpp.
 
user406009
Either manually or via a header with #include.
 
user406009
The implementation can be alone in the fraction.cpp.
 
user406009
class fraction{
public:
    void set(int nume, int denom);
    fraction addedTo(fraction f2);
    fraction subtract(fraction f2);
    fraction multipliedBy(fraction f2);
    fraction dividedBy(fraction f2);
    bool isEqualTo(fraction f2);
    void print();
    fraction();
private:
    int nume;
    int denom;
};
 
user406009
12:31 AM
That is the declaration.
 
user406009
(Also, posting code in this room is fine btw. There are no rules here)
 
user406009
That's the thing you need to copy.
 
hmm...the prof said don't change ANYTHING in the main.cpp file
so idk if having the include statement counts as that
 
user406009
No clue.
 
@btrballin Without it It will not compile.
 
user406009
12:34 AM
But you must either manually copy and paste those files or use a #include. There is no way around it.
 
So is it impossible to leave the main.cpp file exactly the way it is and have it recognize fraction as a class when it's instantiating an object of that class?
 
user406009
@btrballin Correct.
 
@btrballin Yes. Impossible.
Do you have a copy of your assignment you can share?
 
That is your solution, not the assignment.
 
12:37 AM
So my goal for this assignment is: no header file separately and have main method in separate cpp file, but it won't recognize the fraction class in the separate client.cpp file
 
user406009
Well, you could technically #include the main file at the end of your fraction.cpp file.
 
user406009
That's probably not what your professor is looking for though.
 
@Lalaland Excellent solution. One compilation unit.
 
user406009
It is password gated.
 
12:40 AM
user: student pass: objects
 
user406009
@btrballin Note: in future weeks you will be submitting the client program and class in separate files, but for this week you will be putting all of your code, class and client program, into a single file to submit it. The class declaration will come first, followed by the definitions of the class member functions, followed by the client program.
 
user406009
The teacher wants you to move all your code in fraction.cpp into main.cpp
 
oh crap...how did I miss that part lol
he had an include statement above the client he wanted us to copy paste so I assumed it was in a separate file
 
 
10 hours later…
11:06 AM
Hello, is there by chance anyone here who had experience with building chromium-browser from scratch? Trying to make it work on Windows 10 and leads to runtime crashes. Puzzling
 
 
5 hours later…
4:20 PM
not me @Frederik.L
 
 
1 hour later…
5:47 PM
room topic changed to C++: Solve problems and approach solutions [c] [c++] [c++11] [c++17] [c++-faq]
room topic changed to C++: Solve problems and approach solutions [c] [c++] [c++11] [c++14] [c++17] [c++-faq]
 
 
2 hours later…
8:09 PM
pastebin.com/kpdbcyWn I know this is only legal in VC, wondering if there's a way around it. I just don't want to allocate a person object in the heap, because I'm already allocating the superperson object...
 
bump
 
@Phantom is there any reason that prevents you from copying the object
as in, you store the value
 
3 messages moved from Lounge<C++>
 
wow
this rexists
 
@R.MartinhoFernandes It does work in VC 2015. Just tried it.
 
8:11 PM
@Phantom No, it doesn't.
 
@R.MartinhoFernandes I just tried it 10 seconds ago.
 
@Phantom And I don't care.
@Phantom It will break as soon as you look at it funny.
 
5 messages moved from Lounge<C++>
 
so
2 mins ago, by milleniumbug
@Phantom is there any reason that prevents you from copying the object
 
@R.MartinhoFernandes pastebin.com/tv4mKG4v
That's the example I'm trying in VC++
and it outputs 5
 
8:15 PM
Yes, and it's broken.
 
it's broken
 
Try, I dunno, adding auto b = std::make_unique<superperson>(person {});.
Or doing it in a loop.
 
the temporary dies at the end
 
For some reason it doesn't in VC...
 
Anything can make it fail because, well, it's broken.
@Phantom It doesn't work in VC++ any more than it does on other compilers.
It just seems to work because your code doesn't really do anything.
 
8:17 PM
also don't store everything on heap
these two classes aren't even polymorphic
 
In my case I need in the heap... since I don't want it to be destroyed.
But i wanted to allocate once
 
no you don't
 
I don't?
 
copy it
 
It's not a 5 bytes object.
that was just an example
 
8:19 PM
yeah it's 1 byte
well then, what do you store in there
 
@Phantom The only difference VC++ has in this case is that the code compiles despite being broken. It will still break as soon as you whistle.
 
@R.MartinhoFernandes I just tried this: pastebin.com/g3c0VnXj
And it outputs 1000 5's
Wait running it in debugging
trying release now
 
Try adding a destructor to person that changes n to 10.
Talk to me after that.
@Phantom Er, make all the objects different.
 
By the way, I want it to crash as much as you.
 
the crashes are the best case scenario
 
8:24 PM
@Phantom It won't crash.
When it breaks, it will print garbage.
 
I meant that
:D
 
unpredictable behaviour on the even days is what you'll get
 
Adding a constructor did the trick
finally it outputs wrong values :D
 
having crazy values in this code is still one of the better outcomes
you're storing a reference to a temporary that dies
 
Yes I know that
The thing is
What I want to do is something like
the decorator pattern
but the problem is i always need to allocate the object unless it already exists
 
8:36 PM
well there are several options you can take:
a.) just copy it
b.) take ownership of it (see `unique_ptr` or `shared_ptr`)
c.) UNSAFE: take a pointer to it
 
class
{
Number number;
LimitedNumber supernumber(number);
}

class
{
LimitedNumber supernumber(make_unique<Number>());

}
 
and you're doing the last one
 
I dont like either of those options
(in the second example the first number shouldnt be there)
 
well then you're SOL
what's a Number
 
It's just an example
but do you see my point?
using the decorator
i'm force to use either of those
 
8:39 PM
@Phantom not really, no
 
what's the alternative?
 
to what
 
To those 2 options i gave you..
i could have a number inside limitednumber of course
but i dont want to do that
 
what's the sizeof of these
 
It's an old project so not all of them are tiny
and even if they were
i rather not destroy copy objects
because then i'd have to add a mutex or smt
instead i'm using atomics
 
8:42 PM
wait wait wait, have you inherited an old project or sth
 
yeah something like that.
 
anyway, what's your real type
do you actually have mutexes there
you really can't escape this dillema:
9 mins ago, by milleniumbug
a.) just copy it
b.) take ownership of it (see `unique_ptr` or `shared_ptr`)
c.) UNSAFE: take a pointer to it
 
But I was talking
about the decorator pattern.
 
the third option is quite usable, but you need to document that the decorated object must live longer than a decorator
 
If I'm not wrong, the decorator pattern does not create a new object
it uses an existing one
 
8:47 PM
it's irrelevant if the object doesn't have identity
depending on your domain, your decorator can be a template
but this means depending whether it's copied or not, the decorator type is different
and this propagates further
what is your real problem because ~~~patterns~~~ are usually not the answer in C++
copying isn't problematic nowadays since the introduction of move semantics
 
Well, I think that's one of my problems.
I wanted to know if I could be able to use the decorator pattern in C++ without having to allocate objects over and over.
And seems like that's the only way.
I think my question is dumb because I knew from the start what I was trying to do wasn't possible...
I guess I just needed a slap on the face.
 
if you have 1000 objects, and each one of them needs to be decorated, then yes, you need 1000 decorator objects
 
In my example number was allocated with the whole classalready
but if i want to use a decorator then i need to allocate
the number object before passing it to the decorator
or like in my first example
have 2 variables, one the number, and then decorator owns it
and i put a weird name on the number so that i dont use it later
this way i dont have to allocate memory
but it seems retardd...
 
Numbers aren't normally expensive to copy
 
Well I thought we agreed it wasn't a number.
its just an example
but then again the idea of the decorator is to not copy things
I *anXY = new Y(new X(new A));
or maybe it is... i dont know .
 
8:55 PM
the pattern doesn't say anything about this
also don't apply a pattern without a real problem to solve
 
I wanted to learn to use the pattern in C++
But the fact that it doesn't say that u cant copy things
it would be weird to copy it
what if u want to decorate an existing object
without making a new one?
u cant do it anymore...
 
it's not the real part of the problem
as in, the pattern doesn't care
are you familiar with other languages?
Java or C# maybe
because there, every allocation is heap allocation
only after optimization where the JIT can figure out the object isn't used elsewhere it can decide to place it in a different place
if you care, you can see an example of what could be described as a "decorator pattern"
 
Yeah I know a bit about Java/C
 
and here's an example of how you use it: github.com/milleniumbug/wiertlo/blob/master/examples/…
this class adapts an array (i.e. any type that has operator[] defined for it, that is, I can index it)
from 0-based, to 1-based
it copies by default, unless you tell it not to
it's a variation of options: 1 and 3
this is an application of decorator pattern, for a real-world class
 
Well If I undeerstood correctly the first make_one_based creates a new object
 
9:07 PM
yeah, that's your decorator
 
and the latter T is const T& inside the class so it just copies a const ref
That's a simple decorator
Because you can't call any function that modifies it
if T is const T&
 
it's T so by default it copies
BUT
 
But if its const T& you can't.
 
if you pass it std::ref(object), it stores a reference to it
oh, I forgot to pass you the definition of wiertlo::extract_reference
 
When I initially had the problem I knew I could fix it with templates...
But I don't want to abuse them.
I remember Herb Sutter joke saying something like "I havent used templtes in a year, and I'm still alive"
 
9:12 PM
this is the same code pasted in coliru
templates in moderate amount are fine, really
anyway, here, seemingly the problem is solved because you can mix approaches 1, 2 and 3
well, it is solved, but there's a tradeoff, that each type is different
 
Yeah but I wanted to use decorators often
Yeah obv
I wrote that yesterday
im guessing there's one likt it already somewhere
no clue xD
 
a range library? sure, everyone writes their own because, sadly, there isn't one in standard library
there's range-v3 which is quite close to getting included, but still, it has its problems
 
int arr555[10] { 0, 1 ,1, 0, 0 };

auto fnfnfn2 = [](int* it)
{
return (*it) == 1 ? true : false;
};

for (auto n : MakeRangeFilter(arr555, fnfnfn2))
{
std::cout << n << std::endl;
}
that's the usage, it only prints 1 obv
Had to add more things on the class to support c arrays
initially with c++ arrays that have typefes and iterators it was much simple..
 
I actually don't bother supporting C arrays for my private projects
only when I think the utility could be useful for more people, I make it more generic
 
I was thinking in sharing it. Was wondering if was good enough, that's why I wnated to support c arrays.
But if u see the whole thing, there's also a separate class
struct FilterFunctor
{
bool operator()(int* it)
{
return (*it) == 1 ? true : false;
}
};

using RangeFilter = RangeFilterSimple<decltype(arr555), FilterFunctor>;

for (auto n : RangeFilter(arr555))
{
std::cout << n << std::endl;
}
which is more simple
 
9:20 PM
the first one obv
with the lambda
 
But I'd say it's nice to have both
 
as soon you start having stateful functors, you'll see lambda is more convenient
 
Since you can reuse the rangefilter typedef
anywhere and multiple times
like im using it to filter online users
rather than checking each user.isonline()
in the loop
since i have all the connections in the same place...
 
the first one is more elastic though
it can handle both stateful and stateless functors
 
yeah
 
9:22 PM
so I prefer it
 
i initially wrote the second one tho then i realized i would prolly end up needing the other one as well
but i dont know i havent seen such ranger filter anywhere
i think the standard should have it
 
yeah it currently doesn't
but there's a lot of range libraries
Eric Niebler is writing one ericniebler.github.io/range-v3 which currently is most probably the one that we'll see included in the standard
 
am i wrong or those create new objects everytime
i wanted to avoid that, and just filter them as u loop trought the container
with the lambda/functor
oh never mind the view things
 
in this case you can probably use the option 3
with taking a pointer or a reference to it
since most people probably use them locally, the lifetime issues aren't usually a problem
anyway, I gotta go
if you want to goof around then the Lounge<C++> is your place
 
Alright
 
9:33 PM
otherwise if you have questions you may ask on SO or here
 
Okay thanks, later.
 
10:23 PM
Anyone who can help me?

I'm trying to make a queue of vectors, but it says that I miss a trivial destructor and a trivial assignment operator.

boost::lockfree::queue<std::vector<uint8_t>> m_messages;
 

« first day (17 days earlier)      last day (2840 days later) »