How can I convert a variable of type var or var* to var&
I've to use a function which takes an object of var class(suppose there's a class). The example code was given like this:-
testFunc(false, namespace1::namespace2::var(), 100);
in function declaration it says that the second parameter is of type namespace1::namespace2::var&, I can create namespace1::namespace2::var or namespace1::namespace2::var*, but how do I create namespace1::namespace2::var&?
I know that's too basic question but I couldn't figure it out.
Edit:
I've tried using just var, but it gives some odd errors. I am pretty sure that's some kind of fault in the function I am using. Here're the errors:-
Error 3 error C2825: 'CType': must be a class or namespace when followed by '::'
Error 4 error C2039: 'TypeCode' : is not a member of '`global namespace''
Error 5 error C2146: syntax error : missing ',' before identifier 'TypeCode'
Error 6 error C2065: 'TypeCode' : undeclared identifier
Error 7 error C3203: 'CustomType' : unspecialized class template can't be used as a template argument for template parameter 'Base', expected a real type
Edit 2
I thought it'd be hard to answer if I include real code, since it's complicated. But have a look if it helps. The real function's signature is like this:-
virtual bool opRaiseEvent(bool reliable, const Common::Hashtable& parameters, nByte eventCode, nByte channelID=0, int* targetPlayers=NULL, short numTargetPlayers=NULL);
and the example code used the function like this:-
mLoadBalancingClient.opRaiseEvent(false, ExitGames::Common::Hashtable(), 100);
which was working fine. But now I want to add data to the HashTable, so I need to create an object of it and then pass it to the function. It's not accepting a pointer or normal variable. I don't know why it's working with just HashTable().
It means the second parameter is passed by reference. You have to simply pass:
namespace1::namespace2::var
This should work :
const Common::Hashtable param = namespace1::namespace2::var();
opRaiseEvent(false, param, 100);
namespace1::namespace2::var v;
testFunc(false, v, 100);
Related
This is the error I'm getting:
Encounter.h(5,26): error C2061: syntax error: identifier 'TestEnemy'
Encounter.cpp(10,1): error C2511: 'void Encounter::startEnc(TestEnemy)': overloaded member function not found in 'Encounter'
Hero and TestEnemy are classes I've defined in other h and cpp files
Encounter.h:
#pragma once
class Encounter
{
public:
void startEnc(Hero player, TestEnemy e1);
};
Encounter.cpp:
void Encounter::startEnc(Hero player, TestEnemy e1)
{
...
}
I suppose the error could be from another part of my code but visual studio seems to suggest that it's something to do with these. I'll share more code if need be; I just didn't want to inflate this post.
So I've tried to figure out what exactly the professor was writing on the board and how it answers the lab assignment we are to do.
This is the lab assignment:
Create a Hash Table and Hash map that holds all of the WORDS in the (given below) Declaration of Independence.
Handle collisions using the chain method. (Note we will not be modifying this table nor doing deletions!)
Programmatically answer the following questions:
What is the size of your hash table?
What is the longest collision (ie. Chain)
What is the most frequently used word and how did you determine it?
Create a (second) Hash Table that holds all of the LETTERS in the Declaration of Independence.
What is the size of your hash table
What letter has the longest collision?
And this is the pseudo-code with some modifications that I did to fix some errors:
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <list>
using namespace std;
class Translate
{
string word;
public:
int trans(string word);
w = word.charAT(0); //gives a letter
return #num;
};
class HashTable
{
int size();
int collision();
int length();
char fword();
public:
Translate t;
list<string> hashTable[29];
bool insert(string word)
{
hashTable[t.trans(word)].push_back(word);
return true;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
HashTable h;
open file f("hash.txt");
//h.insert(word)
while (!f.eof())
{
h.insert(f.word());
}
cout << h.size;
cout << h.collision.length;
cout << h.fword;
return 0;
}
The errors that I have are:
Error 15 error C1903: unable to recover from previous error(s); stopping compilation
Error 5 error C2014: preprocessor command must start as first nonwhite space
Error 4 error C2059: syntax error : 'return'
Error 13 error C2065: 'f' : undeclared identifier
Error 10 error C2065: 'file' : undeclared identifier
Error 8 error C2065: 'open' : undeclared identifier
Error 6 error C2143: syntax error : missing ';' before '}'
Error 1 error C2143: syntax error : missing ';' before '='
Error 11 error C2146: syntax error : missing ';' before identifier 'f'
Error 9 error C2146: syntax error : missing ';' before identifier 'file'
Error 14 error C2228: left of '.eof' must have class/struct/union
Error 3 error C2238: unexpected token(s) preceding ';'
Error 7 error C2238: unexpected token(s) preceding ';'
Error 12 error C3861: 'f': identifier not found
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Error 19 IntelliSense: '#' not expected here
Error 17 IntelliSense: class "std::basic_string, std::allocator>" has no member "charAT"
Error 21 IntelliSense: expected a ';'
Error 18 IntelliSense: expected a declaration
Error 22 IntelliSense: identifier "f" is undefined
Error 20 IntelliSense: identifier "open" is undefined
Error 16 IntelliSense: this declaration has no storage class or type specifier
I've never used .c_str and I'm still pretty new to C++ so my knowledge is limited. I can tell that there are places that need an identifier but I think there is a better way to create a "open file". My previous knowledge is C#, HTML, and some Python in which C++ is giving me some difficulty in learning and understanding. Any help and/or insight would be greatly appreciated!
Code is too mangled to understand. However, I'm trying my best to help with the little knowledge of mine on C++ and hash.
Proposed Code Modification
Program entry point : instead of int _tmain(int, _TCHAR*), use int main().This should guarantee you the ability to test things out should you migrate to non-windows compiler.
Source : Unicode _tmain vs main
I would like to help with the remainder, however, the code posted is way too unintelligible. Would be kind if the algorithm is posted for reference.
There are a few things you should change:
Assuming trans() is supposed to be a function definition, not a declaration, and the lines following it are supposed to be the body:
Unless you specifically want to copy the passed string, you should use const string& instead of string.
It should have braces.
w is a char.
std::string defines operator[], so it can be indexed like an array.
I'm not sure what #num is (I assume it's from Python, but I'm not familiar with that), so I'm not sure how you intend to calculate the return value.
[I will thus assume that you want to return w, but as an int instead of a char. If this is the case, it would be simpler to just return word[0];.]
There are a few issues with HashTable's members.
Member functions size(), collision(), length(), and fword() are private. This doesn't appear to be intentional.
Member variables t and hashTable are public, when you likely wanted them to be private. Again, this doesn't appear to be intentional.
The functions aren't actually defined anywhere, unless you didn't show their definitions. This will cause a linking error when you call them.
While this doesn't need to be changed, there's no reason for HashTable::insert() to actually return a value, if it's hard-coded to always return true. Also, as mentioned in 1.1 above, the parameter should probably be const string&.
_tmain() and _TCHAR are a Microsoft extensions, which is available on Visual Studio and some (but not all) compilers aiming for compatibility with it (such as C++Builder). If you want your code to be platform-independent, you likely want main(). [Note that this doesn't need to be changed. If you're only compiling with Visual Studio, you can leave it as is. If you want platform independence, you can easily define _tmain and _TCHAR yourself.]
Opening a file:
Neither open nor file are keywords in C++, nor are they types (although FILE is a C type, it doesn't appear to be what you want). You appear to want std::ifstream.
You shouldn't use !f.eof() as a condition in a while loop, because eofbit won't be set until after reading fails.
fstream has no member function word(). However, the extraction operator, operator>>() will read a single word at a time, if given a parameter that can accept one.
HashTable::size(), HashTable::collision(), HashTable::length(), and HashTable::fword() are functions. To call them, you use operator(). If you just use a function's name directly, you don't call it, but instead refer to it (this can be used to create a function pointer or function reference).
int has no member function length(). Therefore, you cannot call h.collision().length(). In C++, if you chain function calls like that, each function in the chain is treated as if it were a member function of the directly preceding type, not the leftmost type; this means that for every function after the first, the return type of the preceding function is used. (In this case, h.collision() returns an int, so .length() attempts to call member function int::length(). int isn't a class type, and thus doesn't have any member functions.)
So, considering these, your code can be modified as follows:
// Assuming your stdafx.h contains "#include <string>" and "#include <tchar.h>".
// If it doesn't, either put them there, or #include them here.
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <list>
// #4: Defining _tmain and _TCHAR
#ifndef _tmain
#define _tmain main
typedef char _TCHAR;
#endif
using namespace std;
class Translate
{
string word;
public:
// #1: Fixing trans().
int trans(const string& word)
{
char w = word[0]; // First letter of word.
return w; // Will be promoted to int.
}
};
class HashTable
{
// #2: Making member functions public, and member variables private.
Translate t;
list<string> hashTable[29];
public:
int size();
int collision();
int length();
char fword();
// #3: Making word a const reference. Changing return type to void.
void insert(const string& word)
{
hashTable[t.trans(word)].push_back(word);
}
};
int _tmain(int argc, _TCHAR* argv[])
{
HashTable h;
// #5.1: Opening the file.
ifstream f("hash.txt");
//h.insert(word)
// #5.2 & 5.3: Reading a word.
std::string word;
while (f >> word)
{
h.insert(word);
}
// #6: Calling functions.
cout << h.size();
cout << h.collision(); // #7: Assuming you wanted to output both h.collision() and
cout << h.length(); // h.length(), I put them on separate lines.
// If you actually DID want h.collision().length(), then
// h.collision() should return a type (or reference to a type)
// with member function length(), or be an instance
// (or reference to an instance) of a class with member function
// length() (instead of being a function).
cout << h.fword();
return 0;
}
You still need to provide bodies for HashTable's member functions, apart from insert(), as well as make any other modifications you desire. You might also want to remove member word from Translate, if it doesn't actually need to store a string.
I'm working on an assignment but I can't figure out why I'm getting these errors:
Error 1 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Error 2 error C2143: syntax error : missing ',' before '&'
Its referring to this line of code
ISBN ( const char* str, const ISBNPrefix &list );
ISBNPrefix is another class in a separate header file, and my professor/school tells us not to change the requirements of the assignment. I just don't understand why I'm getting those 2 errors.
Can anyone clarify?
Forward declaration was needed.
class ISBNPrefix;
class ISBN
{
etc, etc.
};
Error 1 means the compiler sees the line of code as a function declaration without a return type. Maybe you meant
ISBN::ISNB(...);
or
void ISBN(...);
Have you included the other file with #include "otherfile.h" ?
I was implementing boost::intrusive for one of my project on visual C++ 2008 and i stumbled upon a problem. i am using splay hooks for splay_multiset containers. I have defined splay hook publically under MyClass (code below).
#include <boost/intrusive/unordered_set.hpp>
#include <boost/intrusive/splay_set.hpp>
#include <iostream>
using namespace boost::intrusive;
class MyClass
{
int int_;
public:
MyClass(int i)
: int_(i)
{}
splay_set_member_hook<link_mode<normal_link> > memberSplayHook;
//**OPTION-1**
//PROBLEM CODE SEGMENT ++
//typedef member_hook<MyClass, splay_set_member_hook<link_mode<normal_link> >, &MyClass::memberSplayHook> MemberOption;
//typedef splay_multiset<MyClass, MemberOption> MemberMultiSet;
//PROBLEM CODE SEGMENT --
MemberMultiSet mmset;
};
//**OPTION-2**
//WORKING CODE SEGMENT ++
typedef member_hook<MyClass, splay_set_member_hook<link_mode<normal_link> >, &MyClass::memberSplayHook> MemberOption;
typedef splay_multiset<MyClass, MemberOption> MemberMultiSet;
//WORKING CODE SEGMENT --
int main()
{
return 0;
}
The problem is, to use splay_multiset, whatever option i choose (either option-1 or 2, mention in code), in both cases i see compilation errors.
When Option-1 is enabled (option-2 is commented), i see errors below:
1>d:\projects\sampleproject\sample.cpp(21) : error C2327: 'MyClass::memberSplayHook' : is not a type name, static, or enumerator
1>d:\projects\sampleproject\sample.cpp(21) : error C2065: 'memberSplayHook' : undeclared identifier
1>d:\projects\sampleproject\sample.cpp(22) : error C3203: 'member_hook' : unspecialized class template can't be used as a template argument for template parameter 'O1', expected a real type
While, when Option-2 is enabled (option-1 is commented out), i dont see undeclared identifier error msg as these errors coming with option-1. But i do see errors like below (which are obvious).
1>d:\projects\sampleproject\sample.cpp(25) : error C2146: syntax error : missing ';' before identifier 'mmset'
1>d:\projects\sampleproject\sample.cpp(25) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
My question is why i am getting error in first case? What can i do to get pass this issue?
Boost member hooks have (always?) been broken, since they don't compile with Visual C++.
I don't have a VS at hand to check for the precise error message so I might be wrong (but reading 'member hooks' and 'Visual C++' always triggers 'there's a problem'-mode), but do try to check this:
http://permalink.gmane.org/gmane.comp.lib.boost.user/56875
EDIT: Don't take the headline literally -- the same applies to Visual C++ 2010 and 2012. All my member hooks use this workaround; at some point I might even try to understand what it does, or more importantly, how to package it into a more comfortable setup for less "I need to find a previous implementation of this workaround so I can copy-and-modify it"...
I use Multi-thread method in vs2008 ,use c++ language. when I use _beginthreadex function, I got the follow error:
error C2664: '_beginthreadex' : cannot convert parameter 3 from 'unsigned int
(__stdcall *)(void *)' to 'unsigned int (__stdcall *)(void *)'
ps: I use the template on the threadFunc like this:
template<class T>
unsigned int WINAPI closingReconstruction_ThreadFunc(void* pvPara)
{...}
,and then i creat the thread
_beginthreadex(NULL,0,closingReconstruction_ThreadFunc<T>,(PVOID)(tPara+i),0,NULL)
so i get this error , of course , if I remove the template ,it can be work,but i need to use template, are there any methods to solve this.
You forgot to mention the immediately preceding error message,
error C2065: 'T' : undeclared identifier
Let me just state that looking at the first error message first, is generally a good idea.
Cheers & hth.,