Visual C++ : C2143 - visual-c++

why would the following sniplet
#include <atlcom.h>
...
class FormRegionWrapper;
...
// Errorsource :
typedef IDispEventSimpleImpl
<2, FormRegionWrapper, &__uuidof(FormRegionEvents)>
FormRegionEventSink;
will give me the following errors:
// Error ouput:
error C2143: syntax error : missing ';' before '<'
error C4430: missing type specifier - int assumed.
Note: C++ does not support default-int
?!?
(The sniplet is taken from here : Building a C++ Add-in for Outlook 2010 ). My environment : MS Visual Studio 2012 Professinal, and Windows 7-64.
PS 1: Here what the help says about the IDispEventSimpleImpl :
// Help IDispEventSimpleImpl Class : This class provides implementations of
// the IDispatch methods, without getting type information from a type library.
// template <
// UINT nID,
// class T,
// const IID* pdiid
// >
// class ATL_NO_VTABLE IDispEventSimpleImpl :
// public _IDispEventLocator<nID, pdiid>
//
// Requirements
// -------------------
// Header: atlcom.h

Have you included atlcom.h so that the template IDispEventSimpleImpl is defined? Are the declarations of other classes (I assume written by you) used in the template declaration available? And i don't think that forward definition of FormRegionWrapper is enough, can you try and include the declaration of that class, so that template sees it?
UPDATE
Not sure if this will help much, but I replaced the source of FormRegionWrapper.h with the colde below (example from the MSDN page on IDispEventSimpleImpl:
#pragma once
#include "stdafx.h"
#include <vector>
#include <algorithm>
_ATL_FUNC_INFO Event1Info1 = { CC_CDECL, VT_EMPTY, 1, { VT_I4 } };
class CEventHandler;
typedef IDispEventSimpleImpl <1234, CEventHandler, &__uuidof(IDispatch)>
DummySink;
class CEventHandler : public DummySink
{
public:
BEGIN_SINK_MAP(CEventHandler)
SINK_ENTRY_INFO(1234, __uuidof(IDispatch), 1, OnEvent1, &Event1Info1)
END_SINK_MAP()
void __stdcall OnEvent1(LONG l)
{
//ATLASSERT(l == 445533);
if (l != 445533)
OutputDebugString(L"l is not 445533\n");
}
HRESULT Advise1234(IUnknown * punk) {
return IDispEventSimpleImpl<1234, CEventHandler, &__uuidof(IDispatch)>::DispEventAdvise(punk);
}
};
If this works, you can safely assume that the template is being included correctly.

Add this line after the #include directives in FormRegionWrapper.h:
using namespace ATL;

Related

Linked List Creating head causing error C2143: syntax error : missing ';' before '*'

I am trying to make a linkedList but when I create a Head in a separate ADT with class Node Included in it it produces an error error C2143: syntax error : missing ';' before '*'
This is code for ADT of Node
#include <iostream>
#include "LinkedList.h"
using namespace std;
class Node
{
friend LinkedList;
protected:
int data;
Node *next;
public:
Node(int d =-999, Node *n = NULL);
//Mutators
void setData(int d);
void setNext(Node *n);
//Accessors
int getData ();
Node *getNext();
};
This is code for ADT LinkedList
class LinkedList
{
private:
Node *head; //This line is causing that particular error
public:
//some lines of code
};
I am completely lost tried every possible solution but can't figure out what's actually going wrong here.

static constexpr member in-class initialization

Could anyone help me find out what is wrong with in-class initialization of
static constexpr member varaible like in the code below ?
Using Visual Studio 2013
struct hg {
public:
static constexpr float asd = 9.0f;
};
int main() {
return 0;
}
The above code gives the following errors :
Error 1
error C2144: syntax error : 'float' should be preceded by ';'
Error 2
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

googletest integrated in CLR/CLI Compilation error LNK2028

Im totally new to google testing in CLI. Just managed to set up and integrate google test with visual studio 2012. However, when I try to include the header file of my project with my tester.h file (because I want to test the functions in that headerfile), I encountered the following error:
Error 2 error LNK2028: unresolved token (0A0006FB) "public: __thiscall ExpenseTracker::ExpenseTracker(void)" (??0ExpenseTracker##$$FQAE#XZ) referenced in function "private: virtual void __thiscall enter_settings_user_login_Test::TestBody(void)" (?TestBody#enter_settings_user_login_Test##$$FEAEXXZ) C:\Users\Jacky\Desktop\EzXpns3\test_project\main.obj test_project
Error 3 error LNK2019: unresolved external symbol "public: __thiscall ExpenseTracker::ExpenseTracker(void)" (??0ExpenseTracker##$$FQAE#XZ) referenced in function "private: virtual void __thiscall enter_settings_user_login_Test::TestBody(void)" (?TestBody#enter_settings_user_login_Test##$$FEAEXXZ) C:\Users\Jia Wei\Desktop\EzXpns3\test_project\main.obj test_project
I have tried including dependencies gtestd.lib, kernel32.lib, user32.lib, advapi32.lib, Ws2_32.lib
Problem slightly resolved. Instead of having expenseTracker.h and ExpenseTracker.cpp, I placed all my implementation of ExpenseTracker.h in the header file itself and everything compiled nicely and test run. However, my entire project has been built on both header file and cpp file and its not very wise to redo everything for testing purposes right? Could anyone help with this?
Attached below are my header files.
//tester.h
#include "gtest/gtest.h" //include to use Google Unit test's stuff
#include "C:\Users\Jacky\Desktop\EzXpns3\Source - testing\EzXpns2\ExpenseTracker.h"
using namespace std;
class ExpenseTracker;
/************************************************************************/
/* We write test cases here */
/************************************************************************/
TEST(basic_test, add_simple_route)
{
ASSERT_EQ(1, 1);
}
TEST(enter_settings, user_login)
{
ExpenseTracker :: ExpenseTracker();
//loadUserInfo();
string username = "XXX_XXX";
string password = "12345";
//myTracker -> loadUserInfo();
//bool result = myTracker -> login(username, password);
//ASSERT_EQ(true, result);
}
void runTest(int argument_count, char** argument_vars)
{
testing::InitGoogleTest(&argument_count, argument_vars); //initialize GTest
RUN_ALL_TESTS();
std::getchar(); //pause the program after all the testing
}
//main.cpp
#include "tester.h"
#include <cstdio>
using namespace System;
using namespace testing;
int main(int argument_count, char** argument_vars)
{
//int argc;
//char** argv;
//runTest(argc, argv);
//InitGoogleTest(argc, argv);
testing::InitGoogleTest(&argument_count, argument_vars); //initialize GTest
RUN_ALL_TESTS();
std::getchar();
return 0;
}
//ExpenseTracker.h, the file which I want to include
#ifndef _EXPENSETRACKER_H
#define _EXPENSETRACKER_H
#include <string>
#include <vector>
#include "user2.h"
using namespace std;
class ExpenseTracker
{
private:
vector<User*> allMyUsers;
public:
ExpenseTracker(); //empty constructor
void addUser(User*);
int findUser(string);
bool login(string, string);
void loadUserInfo();
User* getUser(string);
int getUserSize();
};
#endif;
You need to provide an implementation for the constructor. In other words, you should change:
ExpenseTracker(); //empty constructor
into:
ExpenseTracker() {} // empty constructor
However, since the constructor is empty you might as well remove it entirely!

Visual C++ (C++/CLI) Forward Declaration with Derivative Classes?

Okay, so I'm running into trouble with Forward Declarations in Visual Studios C++ (C++/CLI).
The code is as follows:
A.h
#include "B.h"
#ifdef B_H
#pragma once
public ref class A : public base_class //base_class is public, memory managed
{
B^ b;
}
#endif
B.h
#define B_H
#pragma once
ref class A;
ref class B
{
A^ a;
}
#include "A.h"
The #ifdef/#pragma guards should keep be keeping a *.h from being read twice, and forcing b.h to be read first, and from the compiler output I'm pretty sure they are. (I'm not even sure the #ifdef/#define is needed with the #pragma once and #include placement)
But, the complier complains of a path/a.h: error C2011: 'class' type redefinition.
See file path/B.h
Should I be doing something with the forward declaration of A because it's a derivative class in the actual class definition, or am I barking up the wrong tree?
Two changes needed:
Add semicolons after the closing brace of the class definitions.
In A.h, move the #pragma once to be the very first line in the file. It's getting screwed up by having this inside the #ifdef block.
Also, note that a simpler way to do this would be to not have either header file include the other, and use forward declarations in both files:
A.h:
#pragma once
ref class B;
public ref class A : public base_class //base_class is public, memory managed
{
B^ b;
};
B.h
#pragma once
ref class A;
ref class B
{
A^ a;
};

How do I resolve: "error C2039: 'cstr' : is not a member of 'std::basic_string"?

#include <stdexcept>
#include <string>
using namespace std;
class ListIndexOutOfRangeException : public out_of_range
{
public:
ListIndexOutOfRangeException(const string & message = "") : out_of_range(message.c_str())
{
}
}; // end ListIndexOutOfRangeException
out_of_range accepts a string reference, so just use
: out_of_range(message)
instead.
edit:
And as others have said, the compiler is telling you you have used message.cstr() instead of message.c_str(). But the method call is unnecessary anyway, just pass the string.

Resources