Compiler can't find DPI related functions - c++

I included all the needed headers (windows.h, winuser.h), but the compiler keeps complaining about "GetDpiForWindow" or "AdjustWindowRectExForDpi" not being defined.
Code example:
#include <iostream>
#include <windows.h>
#include <winuser.h>
using namespace std;
int main()
{
int dpi = GetDpiForWindow(GetConsoleWindow());
return 0;
}
Error:
error: 'GetDpiForWindow' was not declared in this scope; did you mean
'GetTopWindow'?

Related

c++ Incomplete type error when defining AppServiceConnection with WinRT Console App

I am trying to create a AppServiceConnection but when doing so I get a Incomplete Type not allowed
I verified the header file is imported for that class as mention in other stackoverflow questions.
I tried several different attempted to define the AppServieConnection. Am I putting in the wrong place?
The only way it worked was putting it above the main method.
Here is my code
#include "pch.h"
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Foundation.Collections.h>
#include <windows.h>
#include <winrt/Windows.ApplicationModel.Activation.h>
#include <tchar.h>
#include <stdio.h>
#include <ppltasks.h>
#include <appmodel.h>
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <string>
#include <ppltasks.h>
#include <windows.h>
#include <appmodel.h>
#include <malloc.h>
#include <stdio.h>
#include <assert.h>
using namespace winrt;
using namespace concurrency;
using namespace Windows::Foundation;
using namespace std;
using namespace Windows::ApplicationModel::AppService;
AppServiceConnection connection_worked; // defining it here works but cannot call any methods from it
int main()
{
init_apartment();
Uri uri(L"http://aka.ms/cppwinrt");
printf("Hello, %ls!\n", uri.AbsoluteUri().c_str());
}
class NewClass {
private:
Windows::ApplicationModel::AppService::AppServiceConnection connection{nullptr};
Windows::ApplicationModel::AppService::AppServiceConnection connection2;
Windows::ApplicationModel::AppService::AppServiceConnection connection3 = nullptr;
AppServiceConnection connection4;
AppServiceConnection connection5 = nullptr;
AppServiceConnection connection6 = AppServiceConnection();
IAsyncAction InitializeAppServiceConnection() {
}
public:
NewClass() {
}
};
The error is clear, you are missing AppServiceConnection header file.
Please add the header file
#include <winrt/Windows.ApplicationModel.AppService.h>

passing a vector of structs into a function

In a program the following struct is defined in a header file:
\\structs.h
#include <vector>
using std::vector;
using namespace std;
struct cell
{
double x;
vector<int> nn;
};
In a separate source file I define the function:
\\functions.cpp
# define _CRT_SECURE_NO_DEPRECATE
# include <stdio.h>
# include <iostream>
# include <math.h>
# include <vector>
# include "structs.h"
using namespace std;
void initial_position(vector<cell>& cluster, int n)
{
cell tmp;
for (int i = 0; i < n; i++)
{
tmp.x = 1;
cluster.push_back(tmp);
}
}
with a header file:
//functions.h
# include <vector>
using std::vector;
void initial_position(vector<cell>& cluster, int n);
I wish to call this function in the main script:
//main.cpp
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <iostream>
#include <math.h>
#include <vector>
#include "functions.h"
#include "structs.h"
using namespace std;
int main()
{
vector <cell> cluster;
int n = 100;
initial_position(cluster,n);
return 0;
}
but get the following errors:
functions.h(4): error C2065: 'cell': undeclared identifier
functions.h(4): error C2923: 'std::vector': 'cell' is not a valid template type argument for parameter '_Ty'
functions.h(4): error C3203: 'allocator': unspecialized class template can't be used as a template argument for template parameter '_Alloc', expected a real type
main.cpp(14): error C2664: 'void initial_position(std::vector &)': cannot convert argument 1 from 'std::vector>' to 'std::vector &'
What is the source of the errors? it all seems to be well defined.
Put
#include "structs.h"
into functions.h and protect both structs.h and functions.h with include-guards, e.g.
#pragma once
if available.
add
#include "structs.h"
into functions.h since in functions.h compiler doesn't know what cell is.
Just swap
#include "functions.h"
#include "structs.h"
with
#include "structs.h"
#include "functions.h"
Since cell is declared in structs.h and is needed in functions.h
Or better yet, include structs.h in functions.h
You should also not place using namespace std in a header, that is bad practice. It can cause some nasty hard to find bugs, see e.g. Why is "using namespace std" considered bad practice?

using vector in c++ class definition

I have a mysterious problem. I keep getting a ‘vector’ does not name a type error when trying to define a variable in the tour class. The library seems installed correctly, since I can define vectors in main. According to many posts here I have checked for the right vector includes and std namespace. Still I get the error.
main.cpp
#include <vector>
#include <iostream>
#include <cstring>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <cmath>
#include <cstdlib>
#include "tour.h"
using namespace std;
int main () {
//vector declaration works here
return 0;
}
tour.h
#ifndef TOUR_H_
#define TOUR_H_
#include<vector>
using namespace std;
class tour
{
public:
//variables
int teamID;
vector<int> tourseq; //this is where the error occurs
//functions
tour(int);
};
#endif /* TOUR_H_ */
tour.cpp
#include<vector>
#include "tour.h"
using namespace std;
tour::tour(int id){
teamID = id;
}
What could be wrong here?
Instead of writing using namespace std; and vector<int> tourseq; consider writing std::vector<int> tourseq;.
You probably shouldn't put using namespace std; in your code anyway.

Trouble with using class types within other classes

I have absolutely no idea what's going on. I've been looking up explanations for the weirdness going on here but it seems my situation is in some ways unique. I imagined it was the order in which I include my header files in each of my files, but to no avail, I have not found a combination that seems to be the solution.
The exact error I seem to be getting is "log does not name a type" when declaring LogArray[maxLength].
One of my classes, class logmgmt:
class logmgmt
{
private:
static const int maxLength = 500;
log LogArray[maxLength];
int length;
public:
void fillLogs(int index, int iD, std::string date, double startTime, double endTime);
void displayThisLog(int index);
void setLength(int length);
};
Pre-processor directives within logmgmt.cpp:
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
#include "log.h"
#include "Logmgmt.h"
And directives within main.cpp
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
#include "employee.h"
#include "log.h"
#include "employeemgmt.h"
#include "Logmgmt.h"
Remove using namespace std.
That is polluting the global namespace with lots of symbol names that can cause these conflicts.
In your example, the function std::log becomes log. So it can no longer name a global type.

What's wrong with this C++ program?

I just have an header file and and an .cpp file i am just passing an value to function but it gives me an error
main.c
#include "me.h"
#include <iostream>
#include <sstream>
#include <string.h>
using namespace std;
int main()
{
me("http");
}
me.h
#ifndef ME_H_
#define ME_H_
#include <string.h>
class me {
public:
me(std::string u);
virtual ~me();
};
#endif /* ME_H_ */
me.cpp
#include "me.h"
#include <iostream>
#include <string.h>
using namespace std;
me::me(std::string u) {
// TODO Auto-generated constructor stub
cout << "help";
}
me::~me() {
// TODO Auto-generated destructor stub
}
I am getting an error
In file included from ../src/me.cpp:8:
../src/me.h:13: error: expected ‘)’ before ‘u’
../src/me.cpp:12: error: prototype for ‘me::me(std::string)’ does not match any in class ‘me’
../src/me.h:11: error: candidates are: me::me(const me&)
../src/me.h:11: error: me::me()
make: *** [src/me.o] Error 1
#include <string> instead of #include <string.h>
string.h is the C string header, accessible in C++ as <cstring>
<string> is the C++ header that defines std::string
you want #include <string> instead of #include <string.h>