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 a easy struct
struct Test {
std::vector<int> values;
int value;
}
with overloaded << operator
inline std::ostream& operator<<(std::ostream& p, const Test& t)
{
p << "test: ";
for(size_t i = 0; i < t.values.size(); i++) {
std::cout << t.values[i] << " ";
}
p << " value: " << t.value << std::endl;
return p;
}
this works fine when i use the default output. But when i am using my boost logge, shown here Different boost log sinks for every class, it print the values inside my console and the rest inside my file. Anyone has an idea what happens there?
std::cout << t.values[i] << " ";
should be
p << t.values[i] << " ";
Related
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 9 months ago.
The community reviewed whether to reopen this question 9 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
This seems like a simple thing to me, but I have an entity class:
class Entity
{
private:
std::string m_Name;
public:
int x = 0, y = 0;
Entity() : m_Name("Undefined Entity") {
std::cout << m_Name << " Created!\n";
}
Entity(const std::string& name) : m_Name(name) {
std::cout << m_Name << " Created!\n";
}
~Entity() {
std::cout << m_Name << " Destroid\n";
}
const std::string GetName() { return m_Name; }
void PrintCords() { std::cout << x << ' , ' << y << '\n'; }
};
and in my main function I run the PrintCords function:
int main(void)
{
std::unique_ptr<Entity> j = std::make_unique<Entity>("Jeff");
std::cout << j->GetName() << '\n';
j->PrintCords();
}
Yet, when I run the program, I get seemingly random numbers:
021084480
You should compile with -Werror. This turns your warnings into errors. When you do this, you will see the following error:
prog.cpp:25:43: warning: multi-character character constant [-Wmultichar]
void PrintCords() { std::cout << x << ' , ' << y << '\n'; }
^~~~~
Which should indicate to you that something is wrong. If you change this to a string literal:
void PrintCords() { std::cout << x << " , " << y << '\n'; }
---> ^ ^
Then everything will be ok.
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 1 year ago.
Improve this question
Can somebody help find the mistake in my coding? I'm supposed to pass the value 0 to reference variable in the function init to the struct Bus. I wrote the function output to print the values but the value of b.Currentkept coming out as 16. Here is part of the coding:
struct Bus
{
int Current;
int Max;
};
void init(Bus &b)
{
b.Current = 0;
b.Max = 0;
}
void output(Bus &b)
{
cout << "Passengers: \tactual: " << b.Current
<< "\tmaximum: " << b.Max << endl;
}
int main()
{
struct Bus s;
output(s);
return 0;
}
The output:
Passengers: actual: 16 maximum: 0
I tried changing things but nothing seems to work. Any idea how I can fix this? Thanks.
Your code is not really C++.
Having a free init function is not useful. Use a constructor instead or at least specify default values for its members.
struct Bus
{
int Current = 0;
int Max = 0;
};
No need for init.
Also use member functions for Bus. (And dont write struct Bus s;, just say Bus s;
struct Bus
{
int Current = 0;
int Max = 0;
void output(ostream &out)
{
out << "Passengers: \tactual: " << Current
<< "\tmaximum: " << Max << endl;
};
};
int main()
{
Bus s;
s.output(cout);
return 0;
}
Try to call init function after initializing Bus structure
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 3 years ago.
Improve this question
Thank you in advance for reading ! So this is the code :
#include<iostream>
#include<vector>
std::vector<int> MonkeyCount(int n);
int main() {
MonkeyCount(4);
return 0;
}
std::vector<int> MonkeyCount(int n) {
std::vector<int> MonkeyCountV;
for (unsigned int i = 1; i <= n; i++) {
MonkeyCountV.push_back(i);
}
for (unsigned int i = 0; i <= MonkeyCountV.size(); i++) {
std::cout << MonkeyCount.at(i) << " ";
}
return MonkeyCountV;
}
and the error is on line 23 : error C2227: left of '->at' must point to class/struct/union/generic type
Now i red something about this, but i use this from an example i found on the internet on how to print a vector, and in that exaple, in works. The exaple is this :
#include <iostream>
#include <vector>
void print(std::vector<int> const& input);
int main()
{
std::vector<int> input = { 1, 2, 3, 4, 5 };
print(input);
return 0;
}
void print(std::vector<int> const& input)
{
for (unsigned int i = 0; i < input.size(); i++) {
std::cout << input.at(i) << ' ';
}
}
std::cout << MonkeyCount.at(i) << " ";
Should be:
std::cout << MonkeyCountV.at(i) << " ";
The way you have it is trying to do ".at(i)" on the function itself.
The name of your vector is MonkeyCountV but in the cout statement you are using MonkeyCount which is actually the name of your function. Make sure you have your variables name typed correctly.
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 4 years ago.
Improve this question
Debugger sais there is an incompatible iterator.
How i can solve this.
What can cause the problem.
Here is my main code
for (std::list<std::chrono::duration<double, std::milli>>
::iterator it = road.get_times().begin()
;it!= road.get_times().end();it++,z++) //error incompatible iterator
{
*road::file << "Samochod z drogi " << road.get_lane_number() << " pojechal ";
switch (road.get_direction())
{
case'S':
*road::file << "prosto" << std::endl;
break;
case'L':
*road::file << "w lewo" << std::endl;
break;
case'R':
*road::file << "w prawo" << std::endl;
break;
}
*road::file << "Jego czas stania w kolejce wyniosl ";
avarage_time += it->count() / 1000;
*road::file << round(it->count() / 1000) << std::endl;
}
function road.get_times ()
std::list< std::chrono::duration<double, std::milli>> get_times()
{
return times;
}
get_times returns by value, which means that every time you call it you get a new list object. This is what happens in your for loop. You call it 2 times and you get two objects. You can't compare iterators from two different objects. To fix this create one object by calling get_times just once:
auto times = road.get_times();
for (auto it = times.begin(); it != times.end(); ++it, z++)
//...
You also might want to pause and consider if returning by value is the right approach. I can't answer that for you since I don't know what times is and what is the structure of your program.
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 5 years ago.
Improve this question
I've gone over many questions with the same or similar titles, I have changed the code in so many ways I can't even count.... I have an interesting problem.
I have a class for logging that is extremely simple and just writes stuff into a file. The exact same code works in the constructor, but will not work in the member function. I'm stripping out some irrelevant code, the rest is:
private:
std::string logfile_path_;
std::string program_name_;
std::string GetTimestamp() {
timeval tv;
gettimeofday(&tv, NULL);
char cTimestamp[24];
strftime(cTimestamp, sizeof(cTimestamp), "%F %T", std::localtime(&tv.tv_sec));
sprintf(&cTimestamp[19], ".%03d", (tv.tv_usec / 1000)); // write the miliseconds (microseconds/1000) into cTimestamp starting at the 20th character. %03d == pad with 0, for a minimum length of 3, an integer.
return cTimestamp; // function returns std::string so this will be implicitly cast into a string and returned.
}
public:
int log_level_;
SrxDsLog(std::string Logfile_path, std::string program_name, int log_level) {
log_level_ = log_level;
program_name_ = program_name;
logfile_path_ = Logfile_path;
std::ofstream logfile(logfile_path_.c_str(), std::ios::out | std::ios::app);
std::cout << "Logger started, Log file: " << logfile_path_ << std::endl;
logfile << "Logger started, Log file: " << logfile_path_ << std::endl;
return;
}
void WriteLog(std::string Log_message, int Severity = LOG_CRITICAL, std::string Function_name = "") {
if (Severity >= log_level_) {
std::cout << GetTimestamp() << "|" << program_name_ << "|" << Function_name << "|" << GetSeverity(Severity) << "|" << Log_message << std::endl;
std::ofstream logfile(logfile_path_.c_str(), std::ios::out | std::ios::app);
logfile << GetTimestamp() << "|" << program_name_ << "|" << Function_name << "|" << GetSeverity(Severity) << "|" << Log_message << std::endl;
}
}
The question is why is it working in the constructor, but the exact same code is not working in the member function. The std::cout is writing the exact same log message I want, but it's not appearing in the file. The file contains a single line every time the program is run.
In an amazingly unsatisfying turn of events I voted to close my question.
The problem was apparently caused by undefined behavior in unrelated code. And that because I did something that's defined in C++11 but is not in C++03. Apparently you can't call constructors from constructors in C++03....
Because of that, and because the question didn't include the code that was actually at fault, the question seems incredibly bad.
Please close.
int log_level_;
The constructor fails to initialize this class member.
Subsequently, the comparison with this class member results in undefined behavior.