i have following code
uint32 joaat_hash(uchar *key, size_t len)
{
uint32 hash = 0;
size_t i;
for (i = 0; i < len; i++)
{
hash += key[i];
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
return hash;
}
what will be it's equivalent code in c++? i means data types
1>------ Build started: Project: hash_functions, Configuration: Debug Win32 ------
1> hash_function.cpp
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(3): error C2146: syntax error : missing ';' before identifier 'joaat_hash'
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(3): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(3): error C2065: 'uchar' : undeclared identifier
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(3): error C2065: 'key' : undeclared identifier
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(3): error C2275: 'size_t' : illegal use of this type as an expression
1> c:\users\david\documents\visual studio 2010\projects\hash_functions\predefined c++ types (compiler internal)(19) : see declaration of 'size_t'
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(3): error C2146: syntax error : missing ')' before identifier 'len'
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(3): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(3): error C2078: too many initializers
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(3): error C2275: 'size_t' : illegal use of this type as an expression
1> c:\users\david\documents\visual studio 2010\projects\hash_functions\predefined c++ types (compiler internal)(19) : see declaration of 'size_t'
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(3): error C2059: syntax error : ')'
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(4): error C2143: syntax error : missing ';' before '{'
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(4): error C2447: '{' : missing function header (old-style formal list?)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Include <cstdint> (compiler must support C++0x) and change following datatypes:
uint32 -> uint32_t (or std::uint32_t)
uchar -> unsigned char
As mentioned in comment, cstdint won't be available yet in all compilers, most of the time you can use stdint.h however (the normal C99 header, not in std namespace).
Add these to the top of your code:
typedef unsigned char uchar;
typedef unsigned long uint32; // likely, but not guarenteed.
//typedef unsigned int size_t; // should be defined in stdlib.h
If by "equivalent code in C++" you mean if this should be refactored to use language constructs only available in C++ such as templates or classes, I would say it doesn't.
There are no obvious invariants in the function so it doesn't really form the basis of an object.
The nature of the data to be processed doesn't scale to arbitrary types so templates don't seem a good fit either.
I think the comment suggesting the use of stdint.h is very sensible advice and I would also replace those magic numbers with something more implicitly meaningful (int const's, etc.).
That, to me, seems to already be a C++ code. I wonder why you think it's not.
But since C++ doesn't have uint and uchar explicitly defined you'll need to either have this piece of code before that function.
typedef unsigned int uint32;
typedef unsigned char uchar;
Otherwise, rewrite the function like:
unsigned int joaat_hash(unsigned char *key, size_t len)
{
unsigned int hash = 0;
size_t i;
for (i = 0; i < len; i++)
{
hash += key[i];
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
return hash;
}
Related
This piece of code is compiled with no issues:
const int tmp1 = 1, tmp2 = 1;
const bool cmp = (tmp1 == tmp2);
static_assert(cmp, "OK");
This one is fine, too:
const bool cmp = (HUGE_VALF == HUGE_VALF);
static_assert(cmp, "OK");
This one is not:
const auto tmp = HUGE_VALF;
const bool cmp = (tmp == tmp);
static_assert(cmp, "OK"); // <-- error
Error: expression must have a constant value.
The value of variable "cmp" cannot be used as a constant
Same behaviour with static const.
What is wrong? HUGE_VALF is a preprocessor macro, i.e. constant, and cmp is with no doubt a compile time constant...
Environment:
Microsoft Windows 10
Microsoft Visual C++ 2013 & 2019
With this test program I got to the problem:
#include <cmath>
constexpr auto val1 = 1.23f;
constexpr auto val2 = HUGE_VALF;
static_assert(val1 == val1, "OK");
static_assert(val2 == val2, "OK");
int main()
{
}
Result:
Visual C++ 2019 32 bits: OK
Visual C++ 2019 64 bits: OK
Visual C++ 2013 32 bits: OK
Visual C++ 2013 64 bits: Failed
Output:
1> TestConstexpr.cpp(5): error C2144: syntax error : 'auto' should be preceded by ';'
1> TestConstexpr.cpp(5): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1> TestConstexpr.cpp(6): error C2144: syntax error : 'auto' should be preceded by ';'
1> TestConstexpr.cpp(6): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1> TestConstexpr.cpp(6): error C2086: 'int constexpr' : redefinition
1> TestConstexpr.cpp(5) : see declaration of 'constexpr'
1> TestConstexpr.cpp(8): error C2057: expected constant expression
1> TestConstexpr.cpp(9): error C2057: expected constant expression
I'm using a pre-build 1.66 (lib32-msvc-14.0) that I've downloaded. I'm using Visual studio 2015.
I could run a sample without problem.
But in my project as soon as I include it( a simle wrapper), it generates a lot of errors.
here's my simple wrapper:
Net.h :
#ifndef _PCNET_h
#define _PCNET_h
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <boost/asio.hpp>
#include <string>
using boost::asio::ip::tcp;
typedef unsigned char byte;
typedef void(*newDataCallbackPtr)(byte cmd, byte *data, int len);
namespace net {
void init(std::string host, std::string port);
void send(byte cmd, byte *data, int len);
void registerCB(newDataCallbackPtr cb);
void join();
}
#endif
Net.cpp :
#include "Net.h"
const int max_length = 66000;
tcp::socket *s;
std::string delim = ";z|";
std::string recvData;
std::thread *t, *t2;
newDataCallbackPtr CB;
void readThrd();
//Public
void net::init(std::string host, std::string port) {
boost::asio::io_context io_context;
s = new tcp::socket(io_context);
tcp::resolver resolver(io_context);
boost::asio::connect(*s, resolver.resolve(host, port));
t = new std::thread([&io_context]() { io_context.run(); });
t2 = new std::thread(readThrd);
}
void net::send(byte cmd, byte *data, int len) {
byte *buff = new byte[len + 4];
buff[0] = cmd;
memcpy(buff + 1, data, len);
buff[len + 1] = ';';
buff[len + 2] = 'z';
buff[len + 3] = '|';
boost::asio::write(*s, boost::asio::buffer(buff, len + 4));
delete[] buff;
}
void net::registerCB(newDataCallbackPtr cb) {
CB = cb;
}
void net::join() {
t->join();
}
//Prv8
void readThrd() {
while (true)
{
size_t n = boost::asio::read_until(*s, boost::asio::dynamic_buffer(recvData), delim);
byte *buff = new byte[n - 3];
memcpy(buff, recvData.c_str(), n - 3);
recvData.erase(0, n);
CB(buff[0], buff + 1, (int)n - 4);
}
}
the rest of my project compiles without any problem. but as soon as i Include this Net.h (in another .cpp), compilers gives me a lot of errors in winuser and asio headers:
1>------ Build started: Project: pcMode, Configuration: Debug Win32 ------
1> PCutil.cpp
1> Please define _WIN32_WINNT or _WIN32_WINDOWS appropriately. For example:
1> - add -D_WIN32_WINNT=0x0501 to the compiler command line; or
1> - add _WIN32_WINNT=0x0501 to your project's Preprocessor Definitions.
1> Assuming _WIN32_WINNT=0x0501 (i.e. Windows XP target).
1>c:\program files (x86)\windows kits\8.1\include\um\winuser.h(5836): warning C4091: 'typedef ': ignored on left of 'tagINPUT' when no variable is declared
1>c:\program files (x86)\windows kits\8.1\include\um\winuser.h(5836): error C2143: syntax error: missing ';' before 'constant'
1>c:\program files (x86)\windows kits\8.1\include\um\winuser.h(5836): error C2059: syntax error: 'constant'
1>c:\program files (x86)\windows kits\8.1\include\um\winuser.h(5843): error C2061: syntax error: identifier 'LPINPUT'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(74): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(74): error C2059: syntax error: 'constant'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(75): error C2059: syntax error: ')'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(76): error C2976: 'boost::asio::detail::executor_binder_result_type': too few template arguments
1> c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(44): note: see declaration of 'boost::asio::detail::executor_binder_result_type'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(76): error C2143: syntax error: missing ';' before '{'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(76): error C2447: '{': missing function header (old-style formal list?)
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(82): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(82): error C2059: syntax error: 'constant'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(83): error C2059: syntax error: ')'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(84): error C2976: 'boost::asio::detail::executor_binder_result_type': too few template arguments
1> c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(44): note: see declaration of 'boost::asio::detail::executor_binder_result_type'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(84): error C2143: syntax error: missing ';' before '{'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(84): error C2447: '{': missing function header (old-style formal list?)
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(90): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(90): error C2059: syntax error: 'constant'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(91): error C2059: syntax error: ')'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(92): error C2974: 'boost::asio::detail::executor_binder_result_type': invalid template argument for 'T', type expected
1> c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(44): note: see declaration of 'boost::asio::detail::executor_binder_result_type'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(92): error C2143: syntax error: missing ';' before '{'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(92): error C2447: '{': missing function header (old-style formal list?)
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(98): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(98): error C2059: syntax error: 'constant'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(99): error C2059: syntax error: ')'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(100): error C2974: 'boost::asio::detail::executor_binder_result_type': invalid template argument for 'T', type expected
1> c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(44): note: see declaration of 'boost::asio::detail::executor_binder_result_type'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(100): error C2143: syntax error: missing ';' before '{'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(100): error C2447: '{': missing function header (old-style formal list?)
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(109): warning C4544: '<unnamed-symbol>': default template argument ignored on this template declaration
1> c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(108): note: see declaration of '<unnamed-symbol>'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(118): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(118): error C2059: syntax error: 'constant'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(119): error C2059: syntax error: ')'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(120): error C2976: 'boost::asio::detail::executor_binder_argument_type': too few template arguments
1> c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(109): note: see declaration of 'boost::asio::detail::executor_binder_argument_type'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(120): error C2143: syntax error: missing ';' before '{'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(120): error C2447: '{': missing function header (old-style formal list?)
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(124): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(124): error C2059: syntax error: 'constant'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(125): error C2059: syntax error: ')'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(126): error C2976: 'boost::asio::detail::executor_binder_argument_type': too few template arguments
1> c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(109): note: see declaration of 'boost::asio::detail::executor_binder_argument_type'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(126): error C2143: syntax error: missing ';' before '{'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(126): error C2447: '{': missing function header (old-style formal list?)
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(134): warning C4544: '<unnamed-symbol>': default template argument ignored on this template declaration
1> c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(133): note: see declaration of '<unnamed-symbol>'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(144): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(144): error C2059: syntax error: 'constant'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(145): error C2059: syntax error: ')'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(146): error C2976: 'boost::asio::detail::executor_binder_argument_type': too few template arguments
1> c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(109): note: see declaration of 'boost::asio::detail::executor_binder_argument_type'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(146): error C2143: syntax error: missing ';' before '{'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(146): error C2447: '{': missing function header (old-style formal list?)
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(151): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(151): error C2059: syntax error: 'constant'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(152): error C2059: syntax error: ')'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(153): error C2976: 'boost::asio::detail::executor_binder_argument_type': too few template arguments
1> c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(109): note: see declaration of 'boost::asio::detail::executor_binder_argument_type'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(153): error C2143: syntax error: missing ';' before '{'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(153): error C2447: '{': missing function header (old-style formal list?)
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(219): error C2976: 'boost::asio::detail::executor_binder_argument_type': too few template arguments
1> c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(109): note: see declaration of 'boost::asio::detail::executor_binder_argument_type'
1> c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(493): note: see reference to class template instantiation 'boost::asio::executor_binder<T,Executor>' being compiled
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(219): error C2955: 'boost::asio::detail::executor_binder_argument_type': use of class template requires template argument list
1> c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(109): note: see declaration of 'boost::asio::detail::executor_binder_argument_type'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(220): error C2976: 'boost::asio::detail::executor_binder_argument_types': too few template arguments
1> c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(134): note: see declaration of 'boost::asio::detail::executor_binder_argument_types'
1>c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(220): error C2955: 'boost::asio::detail::executor_binder_argument_types': use of class template requires template argument list
1> c:\boost\boost_1_66_0\boost\asio\bind_executor.hpp(134): note: see declaration of 'boost::asio::detail::executor_binder_argument_types'
1> Net.cpp
1> Please define _WIN32_WINNT or _WIN32_WINDOWS appropriately. For example:
1> - add -D_WIN32_WINNT=0x0501 to the compiler command line; or
1> - add _WIN32_WINNT=0x0501 to your project's Preprocessor Definitions.
1> Assuming _WIN32_WINNT=0x0501 (i.e. Windows XP target).
1> Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
as for PCutil. it has a lot of codes, I've removed irrelevant parts. here's the related parts:
PCutil.h:
#ifndef _PCUTIL_h
#define _PCUTIL_h
#include <iostream>
#include <chrono>
#include <thread>
#include <queue>
#include <stdarg.h>
#include "pinNameDef.h"
extern int ardDigitalPin2PinNum[];
#define HIGH 1
#define OUTPUT 1
#define LOW 0
#define INPUT 0
typedef unsigned char byte;
extern byte exE2PR[];
extern byte inE2PR[];
void PCSend(NETCMDS id, int len, byte b1 = 0, byte b2 = 0, byte b3 = 0, byte b4 = 0, byte b5 = 0, byte b6 = 0, byte b7 = 0, byte b8 = 0);
void PCSend(NETCMDS id, int len, byte *data);
void init(char* host, char* port);
void joinNet();
#endif
PCutil.cpp
#include "PCutil.h"
#include "pinNameDef.h"
#include "../spec.h"
#include "../config.h"
#include "Net.h"
byte exE2PR[PCE2PRSIZE];
byte inE2PR[PCE2PRSIZE];
void PCSend(NETCMDS id, int len, byte b1, byte b2, byte b3, byte b4, byte b5, byte b6, byte b7, byte b8) {
byte b[] = { b1,b2,b3,b4,b5,b6,b7,b8 };
net::send(id, b, len + 1);
}
void PCSend(NETCMDS id, int len, byte *data) {
net::send(id, data, len);
}
void netCB(byte cmd, byte *data, int len) {
//some codes
}
void init(char* host, char* port) {
Net init
net::registerCB(netCB);
net::init(host, port);
}
void joinNet() {
net::join();
}
any idea whats wrong here?
Thanks.
I am setting up some framework for a little 2D game. Right now, I just have a few classes, but I am immediately falling into compiler problems.
I have run this program with only the main function, so I can confirm that the Allegro (graphics library) library linking has worked.
I have put all my header files (.h) under the "Header Files" section in the Solution explorer and all my source (.cpp) in the "Source Files" section. I have only modified settings from default as according to this Allegro tutorial: http://wiki.allegro.cc/index.php?title=Windows,_Visual_Studio_2010_and_Allegro_5. It should not have affected anything in a destructive way.
I have several files, so I will list each one. I will skip some code to reduce size and redundancy. When I omit any code or do anything not verbatim, I will put a comment in the code saying so. (EDIT: I actually did not skip any code)
main.cpp
#include "common.h"
int main(int argc, char **argv)
{
ALLEGRO_DISPLAY *display = NULL;
World* world = new World(640, 480);
if(!al_init()) {
fprintf(stderr, "failed to initialize allegro!\n");
return -1;
}
display = al_create_display(640, 480);
if(!display) {
fprintf(stderr, "failed to create display!\n");
return -1;
}
al_clear_to_color(al_map_rgb(0,0,0));
world->draw(display);
al_flip_display();
al_rest(10.0);
al_destroy_display(display);
return 0;
}
common.h
#if !defined(COMMON_INC)
#define COMMON_INC
#include "World.h"
#include "Pane.h"
#include <stdio.h>
#include <allegro5/allegro.h>
#endif
World.h
#if !defined(WORLD_INC)
#define WORLD_INC
#include "common.h"
#include "Pane.h"
class World{
public:
World(int wp, int hp);
void draw(ALLEGRO_DISPLAY* display);
void update();
protected:
private:
Pane* panel;
int heightPix, widthPix;
};
#endif
World.cpp
#include "common.h"
World::World(int wp, int hp){
widthPix = wp;
heightPix = hp;
panel = new Pane(this, 10, 10, 300, 400);
return;
}
void World::draw(ALLEGRO_DISPLAY* display){
panel->draw(display);
return;
}
Pane.h
#if !defined(PANE_INC)
#define PANE_INC
#include "common.h"
class Pane{
public:
Pane(World* w, int xv, int yv, int wi, int he);
World* getWorld();
int getZ();
void draw(ALLEGRO_DISPLAY* display);
ALLEGRO_BITMAP* getBackground();
protected:
void setXY(int xv, int yv);
void setWidthHeight(int wi, int he);
void setZ(int zv);
void setBackground(ALLEGRO_BITMAP* ba);
private:
int z;
int x, y; //pixels
int width, height; //pixels
World* world;
ALLEGRO_BITMAP* background;
};
#endif
Pane.cpp
#include "common.h"
Pane::Pane(World* w, int xv, int yv, int wi, int he){
this->setWidthHeight(wi, he);
this->setXY(xv, yv);
this->world = w;
}
World* Pane::getWorld(){
return world;
}
void Pane::setXY(int xv, int yv){
x = xv;
y = yv;
return;
}
void Pane::setWidthHeight(int wi, int he){\
width = wi;
height = he;
return;
}
void Pane::setZ(int zv){
z = zv;
return;
}
void Pane::draw(ALLEGRO_DISPLAY* display){
if(background != NULL)
al_draw_bitmap(background, x, y, 0);
else{
background = al_create_bitmap(width, height);
al_set_target_bitmap(background);
al_clear_to_color(al_map_rgb(255, 0, 255));
al_set_target_bitmap(al_get_backbuffer(display));
al_draw_bitmap(background, x, y, 0);
}
return;
}
The compiler produces this error report upon building: (I called the game madscientist because it is supposed to be alchemy-themed)
1>------ Build started: Project: MadScientist, Configuration: Debug Win32 ------
1> main.cpp
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(8): error C2061: syntax error : identifier 'World'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9): warning C4183: 'getWorld': missing return type; assumed to be a member function returning 'int'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(11): error C2061: syntax error : identifier 'ALLEGRO_DISPLAY'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(12): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(12): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(12): warning C4183: 'getBackground': missing return type; assumed to be a member function returning 'int'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(18): error C2061: syntax error : identifier 'ALLEGRO_BITMAP'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(24): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(24): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(25): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(25): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.h(10): error C2061: syntax error : identifier 'ALLEGRO_DISPLAY'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\main.cpp(22): error C2660: 'World::draw' : function does not take 1 arguments
1> World.cpp
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(8): error C2061: syntax error : identifier 'World'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9): warning C4183: 'getWorld': missing return type; assumed to be a member function returning 'int'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(11): error C2061: syntax error : identifier 'ALLEGRO_DISPLAY'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(12): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(12): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(12): warning C4183: 'getBackground': missing return type; assumed to be a member function returning 'int'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(18): error C2061: syntax error : identifier 'ALLEGRO_BITMAP'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(24): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(24): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(25): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(25): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.h(10): error C2061: syntax error : identifier 'ALLEGRO_DISPLAY'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.cpp(6): error C2661: 'Pane::Pane' : no overloaded function takes 5 arguments
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.cpp(10): error C2511: 'void World::draw(ALLEGRO_DISPLAY *)' : overloaded member function not found in 'World'
1> c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.h(7) : see declaration of 'World'
1> Pane.cpp
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(8): error C2061: syntax error : identifier 'World'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9): warning C4183: 'getWorld': missing return type; assumed to be a member function returning 'int'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(11): error C2061: syntax error : identifier 'ALLEGRO_DISPLAY'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(12): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(12): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(12): warning C4183: 'getBackground': missing return type; assumed to be a member function returning 'int'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(18): error C2061: syntax error : identifier 'ALLEGRO_BITMAP'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(24): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(24): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(25): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(25): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.h(10): error C2061: syntax error : identifier 'ALLEGRO_DISPLAY'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.cpp(3): error C2511: 'Pane::Pane(World *,int,int,int,int)' : overloaded member function not found in 'Pane'
1> c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(6) : see declaration of 'Pane'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.cpp(9): error C2556: 'World *Pane::getWorld(void)' : overloaded function differs only by return type from 'int *Pane::getWorld(void)'
1> c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9) : see declaration of 'Pane::getWorld'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.cpp(9): error C2371: 'Pane::getWorld' : redefinition; different basic types
1> c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9) : see declaration of 'Pane::getWorld'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.cpp(10): error C2065: 'world' : undeclared identifier
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.cpp(30): error C2511: 'void Pane::draw(ALLEGRO_DISPLAY *)' : overloaded member function not found in 'Pane'
1> c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(6) : see declaration of 'Pane'
1> Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
You have all of the info that I do. I will summarize it for you to save the trouble of looking through everything.
There are classes separated into header interfaces and source implementations
common.h is included by all files. It itself includes all of the class definitions in the other header files
When compiled, the compiler does not recognize classes defined in other header files as data types. Errors, as you would expect, cascade down.
The real-time error checker does not register any errors. When hovering over the word "World" when it is used as a datatype in Pane, for example, it recognizes the type perfectly. The real-time error checker is the feature that red underlines errors before compiler time.
This Visual Studio 2012 Express is new except for the modifications mentioned earlier. The project was created as an empty C++ project. Before many headers and sources were add, the main function compiled correctly.
Thank you for any responses. If you have any questions, please ask. The problems I have with Visual Studio always irk me.
EDITS:
My circular header logic is guarded by the header guards. I don't think this redundancy is a problem.
I tried removing all includes from my header files except for includes to Allegro libraries. This seemed to work better, but there are still weird problems. Why the act of including is causing these data types to error is still a mystery. Here is the new error log:
1>------ Build started: Project: MadScientist, Configuration: Debug Win32 ------
1> main.cpp
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.h(16): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.h(16): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1> World.cpp
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.h(16): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.h(16): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.cpp(6): error C2065: 'panel' : undeclared identifier
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.cpp(11): error C2065: 'panel' : undeclared identifier
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.cpp(11): error C2227: left of '->draw' must point to class/struct/union/generic type
1> type is ''unknown-type''
1> Pane.cpp
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.h(16): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.h(16): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1> Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
EDITS ROUND 2:
I switched my code around so the common.h only includes stdio and Allegro. All the header and source files include common.h and then any class's header file that they are using individually. Pane.cpp include Pane.h and common.h. World.h includes Pane.h and common.h.
Error log reads:
1>------ Build started: Project: MadScientist, Configuration: Debug Win32 ------
1> main.cpp
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9): error C2061: syntax error : identifier 'World'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(10): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(10): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(10): warning C4183: 'getWorld': missing return type; assumed to be a member function returning 'int'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(25): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(25): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1> World.cpp
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9): error C2061: syntax error : identifier 'World'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(10): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(10): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(10): warning C4183: 'getWorld': missing return type; assumed to be a member function returning 'int'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(25): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(25): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.cpp(7): error C2661: 'Pane::Pane' : no overloaded function takes 5 arguments
1> Pane.cpp
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.h(16): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.h(16): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1> Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
This is how I see my include order now:
main includes common
common defines COMMON_INC
common includes stdio and allegro
main includes World.h
World defines WORLD_INC
World includes common, which is blocked. But this should be okay because common is already included
World includes Pane
Pane defines PANE_INC
Pane includes common, which is blocked. But this should be okay because common is already included.
Pane includes World. This is blocked because main already included World. Shouldn't this be okay since World was already included. Why do I even have compiler guards if I need to include files many times over, once for each file that uses it?
EDITS ROUND 3:
I have learned a lot from the answers and comments here. It turns out that, according to Wikipedia, "circular dependencies are often introduced by inexperienced programmers who need to implement some kind of callback functionality." The mere fact that Pane uses World and World uses Pane is a design flaw. In Java, this was all fine for me. It was very easy; I thought that this was the best way to notify the entity that "possesses" an object of the object's actions.
It turns out that this is a bad design scheme. Objects should not have dependencies to their "possessors". Instead, I need to implement a observer system, where the Pane can tell the World that it has updated its state.
Reading wikipedia cleared up any questions that I had, so I now consider this question finished. Thanks to the contributors for putting up for a learning programmer.
You header files cannot be possibly linked correctly. You included your headers in circular fashion. Your header files include common.h, while common.h in turn includes other headers, like World.h and Pane.h.
This is not compilable by any compiler. You have to develop a hierarchy of headers, from low-level headers to high-level headers and make sure that higher-level headers include only lower-level headers. That way you will make sure you have no circular inclusions.
Anyway, why does your common.h include World.h and Pane.h? This looks like an obvious error. common.h is intended to be a low-level header. Let World.h and Pane.h include it, but don't include World.h and Pane.h into common.h.
Note, that header guards do not solve anything in this case. They simply make sure that the inclusion cycle will not get infinite. They break the cycle, but they don't help you to resolve circular dependencies between declarations, if such dependencies exist.
Look what happens in your case when processing main.cpp
main.cpp includes common.h
common.h defines COMMON_INC
common.h includes World.h
World.h defines WORLD_INC
World.h includes common.h. The whole common.h is skipped by include guards, because COMMON_INC got defined at step 2.
World.h includes Pane.h
Pane.h defines PANE_INC
Pane.h includes common.h. The whole common.h is skipped by include guards, because COMMON_INC got defined at step 2.
Pane.h referred to type World. Type World is unknown, since we haven't gotten to its definition in World.h yet. Error!
I think everything will compile even with the circular includes, if you make the following changes
In common.h you need to rearrange order of headers.
#include <allegro5/allegro.h>
#include "World.h"
#include "Pane.h"
#include <stdio.h>
In pane.h, after the #include common.h, add a forward declaration for class World
#include "common.h"
class World;
class Pane{
.....
I think this will make your errors disappear or atleast decrease them substantially.
My answer is based on the original code you put up, not on the Edit.
I use Visual Studio 2005
When I compile, I get this error:
Error 1 error C2146: syntax error : missing ';' before identifier 'mDropEndTime'
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
struct MB_SYN_DROPITEM_UPDATE : MSGBUF
{
long mCid; // Index
unsigned long mItemIdx; // idx
TIMESTAMP_STRUCT mDropEndTime; // This is error line
};
Why doesn't C++ know TIMESTAMP_STRUCT?
TIMESTAMP_STRUCT is something defined in sqlext.h
You must add
#include <sqlext.h>
Because TIMESTAMP_STRUCT is not part of the C++ standard.
I have this code from programming pearls
#include <iostream>
//#include <string>
using namespace std;
template <class T>
void measure(char *text)
{
cout<<"measure"<<text<<"\t";
cout<<sizeof(t)<<"\n";
}
#define MEASURE(T,text){
cout<<text<<"\t";
cout<<sizeof(T)<<"\t";
int lastp=0;
for (int i=0;i<11;i++){
T *p=new T;
int thisp=(int)p;
if (lastp!=0)
cout<<" "<<thisp-lastp;
lastp=thisp;
}
cout<<"n":
}
int main(){
return 0;
}
but there are some mistakes
1>------ Build started: Project: new_practises, Configuration: Debug Win32 ------
1> practises.cpp
1>c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(11): error C2143: syntax error : missing ';' before '<<'
1>c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(11): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(12): error C2143: syntax error : missing ';' before '<<'
1>c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(12): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(12): error C2086: 'int cout' : redefinition
1> c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(11) : see declaration of 'cout'
1>c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(14): error C2059: syntax error : 'for'
1>c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(14): error C2143: syntax error : missing ')' before ';'
1>c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(14): error C2143: syntax error : missing ';' before '<'
1>c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(14): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(14): error C2143: syntax error : missing ';' before '++'
1>c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(14): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(14): error C2086: 'int i' : redefinition
1> c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(14) : see declaration of 'i'
1>c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(14): error C2059: syntax error : ')'
1>c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(14): error C2143: syntax error : missing ';' before '{'
1>c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(14): error C2447: '{' : missing function header (old-style formal list?)
1>c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(21): error C2143: syntax error : missing ';' before '<<'
1>c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(21): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(21): error C2086: 'int cout' : redefinition
1> c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(11) : see declaration of 'cout'
1>c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(22): error C2059: syntax error : '}'
1>c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(22): error C2143: syntax error : missing ';' before '}'
1>c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(22): error C2059: syntax error : '}'
1>c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(24): error C2143: syntax error : missing ';' before '{'
1>c:\users\david\documents\visual studio 2010\projects\new_practises\practises.cpp(24): error C2447: '{' : missing function header (old-style formal list?)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
What do I need to fix?
DeadMG is correct, there are quite a few mistakes here. The most prominent one is that you are not using #define correctly. It requires that all of the code of the function goes on one line.
#define func(A, B) {//function body goes here}
To allow multi-line #defines, use \s at the end of the lines:
#define MEASURE(T,text) {\
cout<<text<<"\t";\
cout<<sizeof(T)<<"\t";\
int lastp=0;\
for (int i=0;i<11;i++){\
T *p=new T;\
int thisp=(int)p;\
if (lastp!=0) cout<<" "<<thisp-lastp;\
lastp=thisp;\
}\
cout<<"n";\
}
(Note I have fixed a few typos here, including one : where a ; should be.)
The other big problem with your code is in the measure function:
void measure(char *text)
{
cout<<"measure"<<text<<"\t";
cout<<sizeof(t)<<"\n";
}
What is t? I assume you mean text, not t.
The following should compile okay.
#include <iostream>
#define MEASURE(T,text) {\
cout<<text<<"\t";\
cout<<sizeof(T)<<"\t";\
int lastp=0;\
for (int i=0;i<11;i++){\
T *p=new T;\
int thisp=(int)p;\
if (lastp!=0) cout<<" "<<thisp-lastp;\
lastp=thisp;\
}\
cout<<"n";\
}
using namespace std;
template <class T>
void measure(char *text)
{
cout<<"measure"<<text<<"\t";
cout<<sizeof(text)<<"\n";
}
int main() {
return 0;
}
As a final, not directly bug-related question to you - why are you using a #define like this? Why not simply write the #define code into the measure method? #defineis usually used to give a short name for a variable or to declare very small functions - and there is a vocal part of the community that thinks they shouldn't be used at all!
In addition to Stephen's answer, I can say that #define is un-C++ish. It becomes useful when you need a variable name that corresponds to a string or something the like. For mostly all other cases, use functions.
Next to that, I must concur with DeadMG's opinion that at least I'm too stupid to see any reason to write this function. But that's another concern :)
If you actually want to measure the length of a string, you can use the function strlen.
What your MEASURE seems to do is print out a series of 11 newly allocated memory addresses. Nice to do when you are studying the memory allocation strategy of your runtime, but otherwise not too useful.
Also don't forget to delete what you have newed.
#define MEASURE(T,text) {\
cout<<text<<"\t";\
cout<<sizeof(T)<<"\t";\
...
can be directly translated into a template function: way easier to write, and to debug.
template< typename T > // assuming you _need_ a variable type of arguments
void MEASURE( const T& text) {
cout<<text<<"\t";
cout<<sizeof(T)<<"\t";
// will write the size of e.g. one character. Maybe
// you should use strlen or the like...
int lastp=0;
for (int i=0;i<11;i++){
T *p=new T;
int thisp=(int)p;
if (lastp!=0) cout<<" "<<thisp-lastp;
lastp=thisp;
}
cout<<"n";
}
"some" mistakes? Whoever wrote that has NFI what on earth they're doing. The program doesn't even produce any meaningful output. ... or any output at all. You could literally not run it or read the source code and gain the same knowledge: zero.
You need to add "\" at the end of the line when your definition across multiple lines.
Below code is compiled with no errors.
#include "stdafx.h"
#include <iostream>
using namespace std;
template <class T>
void measure(char *text)
{
cout<<"measure"<<text<<"\t";
cout<<sizeof(t)<<"\n";
}
#define MEASURE(T,text) { \
cout<<text<<"\t"; \
cout<<sizeof(T)<<"\t"; \
int lastp=0; \
for (int i=0;i<11;i++){ \
T *p=new T; \
int thisp=(int)p; \
if (lastp!=0) \
cout<<" "<<thisp-lastp; \
lastp=thisp; \
} \
cout<<"n": \
}
int main(){
return 0;
}