Add class members dynamically - oop

For my Arduino project, I want to have a class called Buttons, that has six EasyButton instances as members. I want to pass the pins of the EasyButton instances to the Buttons constructor. How can I instantiate the six EasyButton members of my Buttons class in the Buttons constructor?
#include <EasyButton.h>
uint32_t debounce_time = 200;
bool pullup_enable = true;
bool active_low = true;
class Buttons
{
private:
public:
Buttons(uint8_t pin1, uint8_t pin2, uint8_t pin3, uint8_t pin4, uint8_t pin5, uint8_t pin6);
// do the following in the constructor.
EasyButton button1{pin1, debounce_time, pullup_enable, active_low};
EasyButton button2{pin2, debounce_time, pullup_enable, active_low};
EasyButton button3{pin3, debounce_time, pullup_enable, active_low};
EasyButton button4{pin4, debounce_time, pullup_enable, active_low};
EasyButton button5{pin5, debounce_time, pullup_enable, active_low};
EasyButton button6{pin6, debounce_time, pullup_enable, active_low};
};

This can be done with an initialiser list
class Buttons
{
private:
public:
Buttons(uint8_t pin1, uint8_t pin2, uint8_t pin3, uint8_t pin4, uint8_t pin5, uint8_t pin6);
EasyButton button1;
EasyButton button2;
EasyButton button3;
EasyButton button4;
EasyButton button5;
EasyButton button6;
};
Buttons::Buttons(uint8_t pin1, uint8_t pin2, uint8_t pin3, uint8_t pin4, uint8_t pin5, uint8_t pin6)
: button1(pin1, debounce_time, pullup_enable, active_low),
button2(pin2, debounce_time, pullup_enable, active_low),
button3(pin3, debounce_time, pullup_enable, active_low),
button4(pin4, debounce_time, pullup_enable, active_low),
button5(pin5, debounce_time, pullup_enable, active_low),
button6(pin6, debounce_time, pullup_enable, active_low)
{
}

Related

Implementing IServiceProvider and IInternetSecurityManager for WebBrowser control

I am trying to implement the IInternetSecurityManager on a WebBrowser control in order to override some IE security restrictions.
I have extended the WebBrowser control (call it MyWebBrowser) and have subclassed the WebBrowserSite class (call it ExtendedWebBrowserSite), and I've overridden the CreateWebBrowserSiteBase method on MyWebBrowser to return an instance of ExtendedWebBrowserSite.
class MyWebBrowser : System.Windows.Forms.WebBrowser
{
protected override WebBrowserSiteBase CreateWebBrowserSiteBase()
{
return new ExtendedWebBrowserSite(this);
}
}
My ExtendedWebBrowserSite class implements IServiceProvider and IInternetSecurityManager interfaces:
class ExtendedWebBrowserSite : WebBrowserSite, IServiceProvider, IInternetSecurityManager
{
// constructors omitted...
// IServiceProvider implementation
public int QueryService(ref Guid guidService, ref Guid riid, [MarshalAs(UnmanagedType.Interface)] out object ppvObject)
{
if (guidService == SID_IInternetSecurityManager && riid == SID_IInternetSecurityManager)
{
ppvObject = this;
return HRESULTS.S_OK; // = 0x0
}
ppvObject = IntPtr.Zero;
return HRESULTS.E_NOINTERFACE; // 0x80004002
}
// IInternetSecurityManager implementation ommitted...
}
COM Interface Definitions
[ComImport, GuidAttribute("6D5140C1-7436-11CE-8034-00AA006009FA")]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
public interface IServiceProvider
{
int QueryService(ref Guid guidService, ref Guid riid [MarshalAs(UnmanagedType.Interface)] out object ppvObject);
}
[ComImport, Guid("79EAC9EE-BAF9-11CE-8C82-00AA004BA90B")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IInternetSecurityManager
{
[return: MarshalAs(UnmanagedType.I4)][PreserveSig]
int SetSecuritySite([In] IntPtr pSite);
[return: MarshalAs(UnmanagedType.I4)][PreserveSig]
int GetSecuritySite([Out] IntPtr pSite);
[return: MarshalAs(UnmanagedType.I4)][PreserveSig]
int MapUrlToZone([In,MarshalAs(UnmanagedType.LPWStr)] string pwszUrl, out UInt32 pdwZone, UInt32 dwFlags);
[return: MarshalAs(UnmanagedType.I4)][PreserveSig]
int GetSecurityId([MarshalAs(UnmanagedType.LPWStr)] string pwszUrl, [MarshalAs(UnmanagedType.LPArray)] byte[] pbSecurityId, ref UInt32 pcbSecurityId, uint dwReserved);
[return: MarshalAs(UnmanagedType.I4)][PreserveSig]
int ProcessUrlAction([In,MarshalAs(UnmanagedType.LPWStr)] string pwszUrl, UInt32 dwAction, out byte pPolicy, UInt32 cbPolicy, byte pContext, UInt32 cbContext, UInt32 dwFlags, UInt32 dwReserved);
[return: MarshalAs(UnmanagedType.I4)][PreserveSig]
int QueryCustomPolicy([In,MarshalAs(UnmanagedType.LPWStr)] string pwszUrl, ref Guid guidKey, ref byte ppPolicy, ref UInt32 pcbPolicy, ref byte pContext, UInt32 cbContext, UInt32 dwReserved);
[return: MarshalAs(UnmanagedType.I4)][PreserveSig]
int SetZoneMapping(UInt32 dwZone, [In,MarshalAs(UnmanagedType.LPWStr)] string lpszPattern, UInt32 dwFlags);
[return: MarshalAs(UnmanagedType.I4)][PreserveSig]
int GetZoneMappings(UInt32 dwZone, out IEnumString ppenumString, UInt32 dwFlags);
}
Results
The QueryService method is called when the WebBrowser is initialized. However, right after the QueryService method finishes and returns HRESULTS.E_NOINTERFACE, an AccessViolationException is thrown:
AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at System.Windows.Forms.UnsafeNativeMethods.IOleObject.SetClientSite(IOleClientSite pClientSite)
at System.Windows.Forms.WebBrowserBase.TransitionFromLoadedToRunning()
at System.Windows.Forms.WebBrowserBase.TransitionUpTo(AXState state)
at System.Windows.Forms.WebBrowser.get_AxIWebBrowser2()
at System.Windows.Forms.WebBrowser.set_ScriptErrorsSuppressed(Boolean value)
Note: None of the IInternetSecurityManager methods get called, because QueryService never gets the chance to assign an implementation of it. So I omitted the IInernetSecurityManager methods for brevity.
Anyone have any ideas as to why this could be happening?

C++/winRT xaml Keyboard Event handler

The following, when compiled, produces a "1 unresolved externals..."
My question is - what are the proper parameters?
using namespace winrt;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Input;
xaml
KeyDown="Keyboard_keyDown"
.h
void Keyboard_keyDown(Windows::Foundation::IInspectable const& sender,
Windows::UI::Xaml::Input::KeyRoutedEventArgs e);
.cpp
void SettingsPage::Keyboard_keyDown(IInspectable const& sender,
Windows::UI::Xaml::Input::KeyRoutedEventArgs e) {...}
MainPage.xaml
<Page
x:Class="BlankAppKeypaderror.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:BlankAppKeypaderror"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
KeyDown="Keyboard_keyDown"
>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<Button x:Name="myButton" Click="ClickHandler">Click Me</Button>
</StackPanel>
MainPage.h
pragma once
include "MainPage.g.h"
namespace winrt::DemoProblemApp::implementation {
struct MainPage : MainPageT
{
MainPage();
int32_t MyProperty();
void MyProperty(int32_t value);
void Keyboard_keyDown(Windows::Foundation::IInspectable const& sender, Windows::UI::Xaml::Input::KeyRoutedEventArgs e);
void ClickHandler(Windows::Foundation::IInspectable const& sender, Windows::UI::Xaml::RoutedEventArgs const& args);
}; }
namespace winrt::DemoProblemApp::factory_implementation {
struct MainPage : MainPageT
{
}; }
MainPage.cpp
#include "pch.h"
#include "MainPage.h"
using namespace winrt;
using namespace Windows::UI::Xaml;
namespace winrt::DemoProblemApp::implementation {
MainPage::MainPage() {
InitializeComponent();
}
int32_t MainPage::MyProperty() {
throw hresult_not_implemented();
}
void MainPage::MyProperty(int32_t /* value */) {
throw hresult_not_implemented();
}
void MainPage::ClickHandler(IInspectable const&, RoutedEventArgs const&) {
myButton().Content(box_value(L"Clicked"));
}
void MainPage::Keyboard_keyDown(Windows::Foundation::IInspectable const& sender, Windows::UI::Xaml::Input::KeyRoutedEventArgs e) {
}
}
I changed the handlers (,h & ,cpp) to the following and problem exists:
void Keyboard_KeyDown(Windows::Foundation::IInspectable const& sender, Windows::UI::Xaml::Input::KeyRoutedEventArgs const& e);
2018-1207 - included this in the .h file
include
This was missing in the .H file
#include <winrt/Windows.UI.Xaml.Input.h>

Visual C++ fatal error LNK1120: 1 unresolved externals

Every time i try to run this program it gives me an error: EDITED
fatal error LNK1120: 1 unresolved externals
and another error
LNK2001: unresolved external symbol _main
EDITED
Ui.h:
#pragma once
namespace Spammer2 {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Summary for Ui
/// </summary>
public ref class Ui : public System::Windows::Forms::Form
{
public:
Ui(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Ui()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::TextBox^ textBox1;
protected:
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::Button^ button2;
private: System::Windows::Forms::TextBox^ textBox2;
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::Timer^ timer1;
private: System::ComponentModel::IContainer^ components;
private:
/// <summary>
/// Required designer variable.
/// </summary>
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->components = (gcnew System::ComponentModel::Container());
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->button1 = (gcnew System::Windows::Forms::Button());
this->button2 = (gcnew System::Windows::Forms::Button());
this->textBox2 = (gcnew System::Windows::Forms::TextBox());
this->label1 = (gcnew System::Windows::Forms::Label());
this->timer1 = (gcnew System::Windows::Forms::Timer(this->components));
this->SuspendLayout();
//
// textBox1
//
this->textBox1->Location = System::Drawing::Point(12, 13);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(260, 20);
this->textBox1->TabIndex = 0;
this->textBox1->TextChanged += gcnew System::EventHandler(this, &Ui::textBox1_TextChanged);
//
// button1
//
this->button1->Location = System::Drawing::Point(12, 138);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 1;
this->button1->Text = L"Start";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Ui::button1_Click);
//
// button2
//
this->button2->Location = System::Drawing::Point(196, 138);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(75, 23);
this->button2->TabIndex = 2;
this->button2->Text = L"Stop";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this, &Ui::button2_Click);
//
// textBox2
//
this->textBox2->Location = System::Drawing::Point(172, 112);
this->textBox2->Name = L"textBox2";
this->textBox2->Size = System::Drawing::Size(100, 20);
this->textBox2->TabIndex = 3;
this->textBox2->Text = L"100";
this->textBox2->TextChanged += gcnew System::EventHandler(this, &Ui::textBox2_TextChanged);
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location = System::Drawing::Point(62, 115);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(104, 13);
this->label1->TabIndex = 4;
this->label1->Text = L"Interval milliseconds:";
this->label1->Click += gcnew System::EventHandler(this, &Ui::label1_Click);
//
// timer1
//
this->timer1->Tick += gcnew System::EventHandler(this, &Ui::timer1_Tick);
//
// Ui
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 173);
this->Controls->Add(this->label1);
this->Controls->Add(this->textBox2);
this->Controls->Add(this->button2);
this->Controls->Add(this->button1);
this->Controls->Add(this->textBox1);
this->Name = L"Ui";
this->Text = L"Spammereme V2";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void textBox1_TextChanged(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
timer1->Start();
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
timer1->Stop();
}
private: System::Void textBox2_TextChanged(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void label1_Click(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) {
timer1->Interval = System::Int32::Parse(textBox2->Text);
SendKeys::Send(textBox1->Text);
SendKeys::Send("{ENTER}");
}
}
};
Ui.cpp:
#include "StdAfx.h"
#include "Ui.h"
The compiler error will say the line number for the question you originally asked.
However, it looks like you have
namespace Spammer2 {
//lots of usings
public ref class Ui : public System::Windows::Forms::Form
{
//lots of other stuff
}
};
You need to end the class definition with a semicolon, not the namespace:
namespace Spammer2 {
//lots of usings
public ref class Ui : public System::Windows::Forms::Form
{
//lots of other stuff
}; //<-------
} //<-------
Edit:
Since the question has now changed, if this is all your code add a main to UI.cpp
#include "StdAfx.h"
#include "Ui.h"
int main()
{
}

Qt inherit QSpinBox and QPushButton

I would like create my CustomQSpinBox .
This CustomQSpinBox must inherit of QPushButton and QSpinBox
but when I compile this code :
#include <QSpinBox>
#include <QPushButton>
class CustomQSpinBox : public QSpinBox, public QPushButton
{
Q_OBJECT
public:
CustomQSpinBox (QWidget *parent = 0);
~CustomQSpinBox ();
void initMinMax(int min, int max);
void init();
signals:
void needNumpad();
public slots:
void clicked();
};
I get an error :
erreur : C2594: 'static_cast'ÿ: conversions ambigu‰s de 'QObject *' en
'CustomQSpinBox *'
How I must do my inheritance ?
Yes, when the numPad is closed, the value is set in the QSpinBox. The problem is so to open the numPad when I click on the QSpinBox.
For the moment I manage with this code :
#include <QSpinBox>
#include <QPushButton>
#include <QMoveEvent>
#include <QResizeEvent>
class CustomQSpinBox: public QSpinBox
{
Q_OBJECT
public:
CustomQSpinBox(QWidget *parent = 0);
~CustomQSpinBox();
void resizeEvent(QResizeEvent *event);
void moveEvent(QMoveEvent * event);
signals:
void needNumpad();
public slots:
void clicked();
private:
QPushButton * button;
};

Form.cpp(16): error C2061: syntax error : identifier 'Form1'

Im trying to start working with Windows Forms... And I tryed to make programm by lesson... But it doesnt work and I dont understand why. If someone can help me it would be great.
My error:
1>ClCompile:
1> stdafx.cpp
1> AssemblyInfo.cpp
1> Form.cpp
1>Form.cpp(16): error C2872: 'Form1' : ambiguous symbol
1> could be 'Form1'
1> or 'c:\users\mizuru\documents\visual studio 2010\projects\form\form\Form1.h(15) : Form1::Form1'
1>Form.cpp(16): error C2061: syntax error : identifier 'Form1'
1> Generating Code...
1>
1>Build FAILED.
Form.cpp
// Form.cpp : main project file.
#include "stdafx.h"
#include "Form1.h"
using namespace Form1;
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
// Create the main window and run it
Application::Run(gcnew Form1());
return 0;
}
Form1.h
#pragma once
namespace Form1 {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Summary for Form1
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button1;
protected:
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::TextBox^ textBox1;
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->button1 = (gcnew System::Windows::Forms::Button());
this->label1 = (gcnew System::Windows::Forms::Label());
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(113, 181);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location = System::Drawing::Point(104, 13);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(35, 13);
this->label1->TabIndex = 1;
this->label1->Text = L"label1";
//
// textBox1
//
this->textBox1->Location = System::Drawing::Point(113, 110);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(100, 20);
this->textBox1->TabIndex = 2;
this->textBox1->TextChanged += gcnew System::EventHandler(this, &Form1::textBox1_TextChanged);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 262);
this->Controls->Add(this->textBox1);
this->Controls->Add(this->label1);
this->Controls->Add(this->button1);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void textBox1_TextChanged(System::Object^ sender, System::EventArgs^ e) {
label1->Text="";
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
label1->Text=textBox1->Text;
}
};
}
The problem at hand is that your namespace and your form class both share the name Form1. You have to disambiguate the symbol by specifying that it's the class within the namespace.
Application::Run(gcnew ::Form1::Form1());