|
|
Since you know at compile time the array size, why don't you allocate it on the stack?
#include <iostream>
#include <array>
using namespace std;
constexpr size_t SIZE = 3;
class Vector
{
array<int,SIZE> x{};
public:
Vector (const array<int,SIZE> & a)
{
for (size_t n=0; n<SIZE; ++n)
x[n] = a[n];
}
int operator * (const Vector & v) const
{
int result = 0;
for (size_t n=0; n<SIZE; ++n)
result += x[n] * v.x[n];
return result;
}
friend ostream & operator << ( ostream & os, const Vector & v);
};
ostream & operator << ( ostream & os, const Vector & v)
{
for (const auto & i : v.x)
os << i << " ";
return os;
}
int main()
{
array<int, SIZE> x{1,2,3};
array<int, SIZE> y{6,3,9};
Vector v1{x};
Vector v2{y};
cout << "v1 " << v1 << endl;
cout << "v2 " << v2 << endl;
cout << "v1*v2 = " << (v1*v2) << endl;
}
|
|
|
|
|
I'm converting a project from VS2005 to VS2017 and have many errors:
I'm getting many errors for items not being members of System
Windows
Forms
Drawing
Panel
Button
etc....
Is there something missing from my old code or is this not available in VS2017 or is there something that wasn't included?
I think the app creates a form....
Any help to debug would be much appreciated.
Jim
|
|
|
|
|
Is it a managed C++ project type?
|
|
|
|
|
|
Then it would be better to post in the Managed C++/CLI Forum.
|
|
|
|
|
This is silly , but most I/O devices spec sheets data are written as "hex".
If I want to pass this
char *TXBuffer = (char*) 0x04;
to a function it has to be type casted.
Like so
<pre lang="c++">
char *TXBuffer = (char*) 0x04;
int *TEST = (int*)0x04;</pre>
If not I'll get "invalid conversion".
Cheers
Vaclav
|
|
|
|
|
Vaclav_ wrote: char *TXBuffer = (char*) 0x04; No you don't need to do that, and you shouldn't because it is wrong. You are trying to pass the value 0x04 as the buffer address, which will cause an access violation.
If you need a buffer containing the hex value, and you want to send that buffer's address to a function then you need to do one or other of the following:
unsigned char TXBuffer = 0x04; function(&TXBuffer);
unsigned char TXBuffer[] = { 0x04, 0 }; function(TXBuffer);
xxx function(unsigned char* buffer)
{
unsigned char value = *buffer; buffer++; value = *buffer; }
|
|
|
|
|
Thanks Richard, you are a pal.
Appreciate your comments.
|
|
|
|
|
Hi, I wrote a small c program on a microcontroller, 32bit.
I met a weird problem.
I wrote code like:
const char strtest[] = "Test123\r\n"; UART_print(strtest);
the output is fine, I can see the output on Teraterm(RS232 interface tool), like:
Test123
Test123
..
But when I try code:
char str123[] = "Test123\r\n";
UART_print(strtest);
The output is only the first element:
TTTTTTTT
Any hint is appreciated.
|
|
|
|
|
focusdoit wrote: char str123[] = "Test123\r\n";
UART_print(strtest); Where is str123 used?
What is the signature of UART_print() ?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
Please show the code for the UART_print function.
|
|
|
|
|
focusdoit wrote: But when I try code:
char str123[] = "Test123\r\n";
UART_print(strtest);
You defined the variable str123 but you print the strtest. Where and how is this strtest defined?
|
|
|
|
|
It's memory allocation problem. the MCU has 2 cpu inside, and one set of peripheral units, compiler put str123 in DMA Access prohibited area.
Embedded programming, too many tricks.
|
|
|
|
|
I like to rebuild my Raspberry Pi code to have "top down" structure.
On top would be the device "primitives" class - for example LCD draw circle
In middle I like to have hardware specifics, maybe even protocol (SPI,I2C) specifics - BCMx processor
And finally the access to I/O "pins"
I was thinking of "cascaded C++ " hierarchy but it would not be C++ hierarchy for real.
Or just simple top class having supporting class as variable. Not sure how flexible that would be.
The idea is to be able to add another "top" device and reuse the supporting classes.
I hope this makes sense and if I have asked this before - just ignore OF.
Thanks
Cheers Vaclav
|
|
|
|
|
It actually more complex than that because on a Pi2,Pi3 you have 4 cores so you will have spinlocks/semaphores on all the devices.
What are you doing with cores 1,2,3 at the moment are they still parked or in use?
I assume your code isn't under linux given you have dragged the devices out to objects
Assuming baremetal I would strongly suggest you look at circle
GitHub - rsta2/circle: A C++ bare metal environment for Raspberry Pi with USB[^]
I am not sure you can develop much of a hierarchy it all needs to be fairly flat if you want to use the multicores, welcome to multicore programming
In vino veritas
modified 9-May-18 11:28am.
|
|
|
|
|
I have been taking the free CS50x course from Harvard and just completed the Mario more comfortable problem. This is my code and I wanted to know how the right side of hashes are left aligned even though the code is the same for both left and right hashes.? To be honest I’m new to this and thought it would simply print out two right aligned pyramids with a gap in between.
// Prints bricks, sized as specified by user
#include <cs50.h>
#include <stdio.h>
int main(void)
{
// Prompt user for a positive number to use as height of pyramid
int h;
do
{
h = get_int("Height: ");
}
while (h < 0 || h > 23);
// Print out this many rows
for (int i = 0; i < h; i++)
{
// Print out this many spaces
for (int j = 0; j < h - i; j++)
{
printf(" ");
}
// Print left hashes
for (int j = 0; j < i + 2; j++)
{
printf("#");
}
// Print gap
printf(" ");
// Print right hashes
for (int j = 0; j < i + 2; j++)
{
printf("#");
}
printf("\n");
}
}
|
|
|
|
|
Faith Burnett wrote: To be honest I’m new to this and thought it would simply print out two right aligned pyramids with a gap in between. Those two for() loops are printing the same NUMBER of hashes from where the stdout stream happens to be at the point the for() loop starts printing. Since the stdout stream is at "column" 0 before the first for() loop, the first set of hashes is right justified (preceded with spaces), and since the stdout stream is at "column" H+2 after the first two inner for() loops run, the second set of hashes always starts in the same "column."
You might want to consider changing your for() loop boundaries so that the pyramid has a "pointed" top at level 1, and no leading/trailing spaces at level h :
for (int i = 0; i < h; i++)
{
...
for (int j = 0; j < h - i - 1; j++)
...
for (int j = 0; j < i + 1; j++)
...
for (int j = 0; j < i + 1; j++)
...
}
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
modified 6-May-18 0:28am.
|
|
|
|
|
Thanks for the help David!
|
|
|
|
|
I like to verify actuall value passed from char array to struct pointer
buf array declaration
unsigned char buf[32]; // = { 0x00, 0x04 };
value passed unsigned char 0x04;
buf[0] = command; // passed 0x04 software reset
transmit buffer assignment
xfer[0].tx_buf = (unsigned long) buf;
Now I like to use cout to print the actual value in tx_buf.
From struct declaration I know "tx_buf" is a unsigned long pointer.
Not sure how to access the required value
this gives me the pointer as expected:
<pre lang="c++">cout << "xfer[0].tx_buf " << xfer[0].tx_buf<< endl;
Once I know how to get first value I can probably figure out how to get the whole buf[32] array values.
Thanks for help
Cheers Vaclav
|
|
|
|
|
You need to print each byte independently, after casting them to int , since the char values are unprintable. Something like:
cout << hex << (int)buf[0] << endl;
Use a loop to print more than one value.
|
|
|
|
|
Thanks, but...
I know how to get buf values, what I wanted to verify is that the buf values are actually passed to the struct.
I am having issues verifying that the struct has all of the necessary data.
Some are "straight" values, most via pointers.
BTW
When I use "modifier" hex , not sure I am using correct term as always, in cout << hex << (int)buf[0] << endl;,
then following usage WITHOUT such modifier will continue to print the data in hex
cout << int)buf[0] << endl;
Nice but it can get out of control.
|
|
|
|
|
Vaclav_ wrote: I am having issues verifying that the struct has all of the necessary data. You need to be specific, we cannot guess what these issues may be. And, TBH, my previous reply was also based on a bit of guesswork.
See <iomanip> functions[^] for details on using the hex manipulator.
|
|
|
|
|
I just like <b>to see the value</b> passed to struct pointer.
I can deal with what the functio0n does with it myself.
Perhaps it is all working correctly and my problems are elsewhere.
|
|
|
|
|
I am trying to wrap a c++ class for c. The function uses pass by reference.
Example
Test::funcref(int & h,BSTR & b)
void wrap_funcref(wrap_t *m, int h, BSTR)
{
Test *obj;
if (m == NULL)
return;
obj = static_cast<test *="">(m->obj);
obj->funcref(&h,&b);
}
Not sure if I need to change the c++ functions for *
|
|
|
|