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.
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 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 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.
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] << " ";
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Improve this question
I have 3 maps with a string as key and a vector as value.
std::map<std::string, std::vector<int> > m_attri; ///< Attributes of type int
std::map<std::string, std::vector<double> > m_attrd; ///< Attributes of type double
std::map<std::string, std::vector<std::string> > m_attrs; ///< Attributes of type std::string
I have a function that resizes them all, calles SetNum()
void SetNum(size_t n)
{
std::cout << "setting num: " << n << std::endl;
m_num = n;
for (auto attr : m_attri) {
attr.second.resize(n, 0);
std::cout<<attr.second.size();
}
for (auto attr : m_attrd) {
attr.second.resize(n, 0.0);
std::cout<<attr.second.size();
}
for (auto attr : m_attrs) {
attr.second.resize(n, "");
std::cout<<attr.second.size();
}
std::cout<<std::endl;
}
this all seems to work, when is say setnum(1), I get a bunch of 1s as output.
Now the problem lies here, I have another function called Set
template<typename T>
inline void AttributeContainer::Set(size_t node_idx, const std::string& name, T value)
{
std::cout << GetNum() << node_idx << ',' << std::endl;
if( node_idx >= GetNum()){
SetNum(node_idx+1);
}
auto attr_kv = Attributes<T>().find(name);
if (attr_kv != Attributes<T>().end()) {
std::cout<<"Containersize: " << attr_kv->second.size() << ',' << "id:"<< node_idx<< std::endl;
attr_kv->second.at(node_idx) = value;
std::cout<<"ContainersizeDone: " << attr_kv->second.size() << std::endl;
} else {
throw std::runtime_error("AttributeContainer::Set(): attribute not found: " + name);
}
}
node_idx is 0 and the vector is found. But it seems that the resize didn't work, because, altough I get output that confirms it is resized, i get a out_of_range exception from at and the line above shows that the size is 0. Now this is weird, as I thought that I succesfully resized them?
In SetNum, you are iterating over your map by value (see this post)
Consequently, you are applying resize on copies of your stored vectors.
What you want is to use auto& :
void SetNum(size_t n)
{
std::cout << "setting num: " << n << std::endl;
m_num = n;
for (auto& attr : m_attri) {
attr.second.resize(n, 0);
std::cout<<attr.second.size();
}
for (auto& attr : m_attrd) {
attr.second.resize(n, 0.0);
std::cout<<attr.second.size();
}
for (auto& attr : m_attrs) {
attr.second.resize(n, "");
std::cout<<attr.second.size();
}
std::cout<<std::endl;
}
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I was wondering, and some people with a lot more knowledge than me will probably know the answer :
Why isn't the C (in my case its for C++) preprocessor more complete ?
What I mean is why couldn't we use for example C++ as the preprocessor language ? It would allow us to do so much more about classes, generate dynamic code etc...
Ideally I would want to call a C++ function just like a preprocessor macro, with a concrete example, I would like to do things like:
#void generateVariable(std::string type, std::string name) {
# if (name[0] == 'p')
# cout << "protected:" << endl << type << " " << name << ";" << endl;
# if (name[0] == 'm')
# cout << "public:" << endl << type << " " << name << ";" << endl;
# std::string prefix = name;
# prefix.erase(2, npos);
# name.erase(0,2);
# name[0] = toupper(name[0]);
# cout << "public:" << endl << type << "get" << name << "() const { return " << prefix+name << "; }" << endl;
#}
So that I could call
class A {
generateVariable(static const int, p_age)
}
And it would generate
class A {
protected:
static const int p_age;
public:
static const int getAge() const { return p_age; }
}
Are there actually ways to do this kind of thing, whithout parsing the whole file with a scripting language and rewriting it ?
The preprocessor actually can do this (though I wouldn't recommend to do so):
#define generateVariable(__type__, __name__) \
protected: __type__ __name__; \
public: __type__ get##__name__() { return __name__; }
class A {
generateVariable(static const int, p_age);
}
get##__name__() will expand to getp_age() though ...