I have created a class and having CString data member.
class Clistof
{
CString m_exe;
public:
Clistof(CString temp){m_exe = temp}
};
error : C3646 'exe': unknown override specifier
Any idea ?
Thanks,
You probably did not include a header that contains the definition for CString. Try including #include <atlstr.h> if you haven't done so already.
Also, please note that you forgot to add a ; after the assignment statement in your constructor: instead of Clistof(CString temp){m_exe = temp} you should write Clistof(CString temp) { m_exe = temp; }.
Related
I am trying to send a vector of "struct" per message, but when defining the message field the following error is generated:
Entering directory '/home/veins/workspace.omnetpp/veins/src'
veins/modules/application/clustertraci/ClusterTraCI11p.cc
veins/modules/application/clustertraci/ClusterTraCI11p.cc:160:40: error: no viable conversion from 'vector' to 'const vector'
frameOfUpdate->setUpdateTable(updateTable);
I read chapter 6 of the OMnet ++ manual, but I don't understand how to solve this problem.
Implementation with error
Message Code (MyMessage.msg):
cplusplus {{
#include "veins/base/utils/Coord.h"
#include "veins/modules/messages/BaseFrame1609_4_m.h"
#include "veins/base/utils/SimpleAddress.h"
#include <iostream>
#include <vector>
struct updateTableStruct {
int car;
char update;
};
typedef std::vector<updateTableStruct> UpdateTable;
}}
namespace veins;
class BaseFrame1609_4;
class noncobject Coord;
class noncobject UpdateTable;
class LAddress::L2Type extends void;
packet ClusterMessageUpdate extends BaseFrame1609_4 {
LAddress::L2Type senderAddress = -1;
int serial = 0;
UpdateTable updateTable;
MyApp.cc:
void ClusterTraCI11p::handleSelfMsg(cMessage* msg) {
if (ClusterMessage* frame = dynamic_cast<ClusterMessage*>(msg)) {
ClusterMessageUpdate* frameOfUpdate = new ClusterMessageUpdate;
populateWSM(frameOfUpdate, CH2);
frameOfUpdate->setSenderAddress(myId);
frameOfUpdate->setUpdateTable(updateTable);
sendDelayedDown(frameOfUpdate, uniform(0.1, 0.02));
}
else {
DemoBaseApplLayer::handleSelfMsg(msg);
}
}
Part of code for analysis in MyApp.h:
struct updateTableStruct {
int car;
char update;
};
typedef std::vector<updateTableStruct> UpdateTable;
UpdateTable updateTable;
You experience a type mismatch: In MyApp.h you define the type UpdateTable, and you do so in MyMessage.h. While these both types have the same content and appear to have the same name, I assume this is not actually the case: one type is UpdateTable (defined at global scope in the file generated based on your message) and the other is MyApp::UpdateTable (defined in your application, assuming you are omitting the class definition in the code you show).
Therefore, the types are different, and they cannot be converted into each other implicitly. In this case this might appear a bit counter-intuitive, as they have exactly the same definition, but they do not have the same name. In the following example the reasoning is shown: Two different types that share the same definition should not necessarily be implicitly convertible into each other:
struct Coordinate {
int x;
int y;
};
struct Money {
int dollars;
int cents;
};
void test() {
Coordinate c;
Money m = c;
}
Gives the following error message:
test.cc:13:8: error: no viable conversion from 'Coordinate' to 'Money'
Money m = c;
^ ~
test.cc:6:8: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'Coordinate' to 'const Money &' for 1st argument
struct Money {
^
test.cc:6:8: note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'Coordinate' to 'Money &&' for 1st argument
struct Money {
^
1 error generated.
Edit:
The solution to your specific problem is to remove one of the definitions and include the remaining definition when using it, so you can either remove the UpdateTable definition from the message and include the App header instead, or remove the UpdateTable definition from the App and include the message instead.
suppose I have a class like this and I would like to create a string with a
specific capacity. I tried doing the following but that did not work.
Any suggestions ? I know I could do it in the constructor but would like to do it during the declaration if possible.
class foo
{
std::string bar = std::string().resize(45);
}
I get the error
main.cpp: In function 'int main()':
main.cpp:8:46: error: conversion from 'void' to non-scalar type 'std::__cxx11::string {aka std::__cxx11::basic_string}' requested
std::string test = std::string().resize(45);
In C++, you probably don't want to "chain" methods unless previous methods have the correct return type.
As suggested by #James Maa, you can do simply use the constructor.
In c++11 we have new feature called move constructor so
string str = string();
doesn't cause extra time.
http://en.cppreference.com/w/cpp/language/move_constructor
with move constructor, the program would directly use the address of the temporary constructed string after = sign, without making a copy.
The problem is that resize()function in c++ actually returns void
basic_string( size_type count,
CharT ch,
const Allocator& alloc = Allocator() );
This constructor might be something you are directly interested in.
You can do something with
std::string str(45, ' ');
When I initialize the std::string member of a class calling its C string constructor, I receive the following errors:
error: expected identifier before string constant
error: expected ',' or '...' before string constant
Although, the program compiles successfully when I use copy initialization or list initialization.
class Element
{
private:
std::string sName_("RandomName"); // Compile error
std::string sName_ = "RandomName"; // OK
std::string sName_{"RandomName"}; // OK
}
What seems to be the problem?
UPDATE
Now I realize this is a stupid question, because, as #p512 says, the compiler will see it as a erroneous method declaration. But I think this question should remain for other people that will do the same thinking error.
std::string sName_("RandomName");
This is an erroneous function declaration - at least that's what the compiler makes of it. Instead you can use something like this:
std::string sName_ = std::string("RandomName");
You can also use initializer lists in the constructor of your class:
class A {
public:
A() : sName_("RandomName") {}
std::string sName_;
};
You can find more on that here: http://en.cppreference.com/w/cpp/language/initializer_list
#include <cstdio>
struct A {
void foo(int) { printf("this is the wrong function\n"); }
void foo() { printf("this is the right function\n"); }
};
int main() {
auto method = &A::foo; // c++ why don't you me allow to give argument types?
A a;
(a.*method)();
}
I know this little example works fine with just replacing auto with an explicit type, but that is not, what I am looking for. I would like to tell c++ on the right side of the equals, which method I want.
The compiler cannot guess which method you refer to unless you specify which overload you are interested in, by explicitely writing its prototype. You can do that either by explicitely typing your variable, like you said:
void (A::*foo)() = &A::foo;
void (A::*fooInt)(int) = &A::foo;
Or you can use a cast on the right hand side of the initialization:
auto foo = static_cast<void (A::*)()>(&A::foo);
auto fooInt = static_cast<void (A::*)(int)>(&A::foo);
You can't use auto here, as it would be ambiguous. You need to explicitly type your variable or use a cast on the right-hand side to restrict the matching to only one of the two candidates.
Hi I am new to Visual Studio 2010
When I try build my code it tells me:
count.cpp(18): error C2065: 'Value' : undeclared identifier
That is my count.cpp code giving out the error:
#include "StdAfx.h"
#include "count.h"
count::count(void): Value(0), ResetValue(0){} //constructor
count::~count(void){} //destructor
int GetValue(){return Value;} //Accessor to get value by another class
Here is my count.h code:
#pragma once
class count
{
public:
count(void);
virtual ~count(void);
int GetValue();
private:
int Value;
int ResetValue;
};
It's a count object that's going to be used by a countWindow object.
So I want an "GetValue" accessor and make use of a pointer in my countWindow dialog window.
Is the variable Value not declared and initiated on the line 12 when I actually make use of the constructor?
Or is it not link to the declaration in the count.h file?
Because if I create another void function to just do a Value++, Visual studio seems fine with.
Thank you for your time!
GetValue is being declared as a global not a member of the count class. Preface the GetValue declaration with count::
int count::GetValue(){return Value;} //Accessor to get value by another class
int count::GetValue(){return Value;} //Accessor to get value by another class
Note the count::