Angular 2 Basic Authentication not working

Simplified version to add custom headers to your request: import {Injectable} from ‘@angular/core’; import {Http, Headers} from ‘@angular/http’; @Injectable() export class ApiService { constructor(private _http: Http) {} call(url): Observable<any> { let username: string = ‘username’; let password: string = ‘password’; let headers: Headers = new Headers(); headers.append(“Authorization”, “Basic ” + btoa(username + “:” + password)); … Read more

c++ template and header files [duplicate]

Headers. It’s because templates are instantiated at compile-time, not link-time, and different translation units (roughly equivalent to your .cpp files) only “know about” each other at link-time. Headers tend to be widely “known about” at compile-time because you #include them in any translation unit that needs them. Read https://isocpp.org/wiki/faq/templates for more.

C++ class header files organization

Some general guidelines: Pair up your interfaces with implementations. If you have foo.cxx, everything defined in there had better be declared in foo.h. Ensure that every header file #includes all other necessary headers or forward-declarations necessary for independent compilation. Resist the temptation to create an “everything” header. They’re always trouble down the road. Put a … Read more

cmath vs math.h (And similar c-prefixed vs .h extension headers)

I’ve seen some information about differences between things like iostream vs iostream.h. [iostream.h] is not a standard header. it is not an example of the issue you’re raising. [cmath] defines symbols in the std namespace, and may also define symbols in the global namespace. [math.h] defines symbols in the global namespace, and may also define … Read more

Define constant variables in C++ header

You could simply define a series of const ints in a header file: // Constants.h #if !defined(MYLIB_CONSTANTS_H) #define MYLIB_CONSTANTS_H 1 const int a = 100; const int b = 0x7f; #endif This works because in C++ a name at namespace scope (including the global namespace) that is explicitly declared const and not explicitly declared extern … Read more

Adding header to all request with Retrofit 2

OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); httpClient.addInterceptor(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request().newBuilder().addHeader(“parameter”, “value”).build(); return chain.proceed(request); } }); Retrofit retrofit = new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create()).baseUrl(url).client(httpClient.build()).build();

Disable warning: the `gets’ function is dangerous in GCC through header files?

The obvious answer is to learn from what the compiler is trying to tell you – you should never, ever, use gets(), as it is totally unsafe. Use fgets() instead, which allows you to prevent possible buffer overruns. #define BUFFER_SIZE 100 char buff[BUFFER_SIZE]; gets( buff); // unsafe! fgets( buff, sizeof(buff), stdin ); // safe

Cyclic dependency between header files

In the headers, forward declare the member functions: class Node { Tree * tree_; int id_; public: Node(Tree * tree, int id); ~Node(); void hi(); }; In a separate .cpp file that includes all the required headers, define them: #include “Tree.h” #include “Node.h” Node::Node(Tree * tree, int id) : tree_(tree), id_(id) { tree_->incCnt(); } Node::~Node() … Read more